Javascript 不是视图切换后的功能
我在使用react-native路由器流量切换视图时遇到react-native问题Javascript 不是视图切换后的功能,javascript,android,react-native,react-native-router-flux,Javascript,Android,React Native,React Native Router Flux,我在使用react-native路由器流量切换视图时遇到react-native问题 mapMarkerGen(){ if(mapMarkersFetched){ _this = this; return mapMarkers.map(function(marker, i){ return( <MapView.Marker key={i} coordinate={{ longitude: pars
mapMarkerGen(){
if(mapMarkersFetched){
_this = this;
return mapMarkers.map(function(marker, i){
return(
<MapView.Marker
key={i}
coordinate={{
longitude: parseFloat(marker.lo),
latitude: parseFloat(marker.la),
}}
onPress={() =>{_this.getMarkerInfo(marker.id, i);}}
title={_this.state.title}
description={_this.state.info}
onCalloutPress={function(){Actions.place({placeID: marker.id});}}
>
</MapView.Marker>
);
});
}
}
mapMarkerGen(){
如果(mapMarkersFetched){
_这个=这个;
返回mapmarks.map(函数(marker,i){
返回(
{{u this.getMarkerInfo(marker.id,i);}
title={u this.state.title}
description={u this.state.info}
OnCalOutPress={function(){Actions.place({placeID:marker.id});}
>
);
});
}
}
当我在视图之间切换后,如果我单击callout并返回,我会得到错误消息\u this.getMarkerInfo不是一个函数
“反应”:“^16.0.0-alpha.12”
“本机反应”:“^0.45.1”
“react native router flux”:“^3.40.1”问题是您正在将
\u this=this
定义为全局变量。您可能会覆盖该值,当您想在onPress
回调中访问该值时,该值指向另一个此
引用,该引用不定义被调用的函数
要解决此问题,请将上述代码替换为:const\u this=this
我建议使用ES2015的箭头函数,就像使用
onPress
回调一样,您不需要自己处理此引用。如何调用mapMarkerGen
?this
绑定是否正确?mapMarkerGen是从如下不同的函数调用的:{this.mapMarkerGen()}是函数中的调用,其中this
范围与您在发布的代码中期望的范围不同吗?最好的做法是将函数绑定到构造函数中,并始终调用绑定的函数。您可以执行类似于this.mapMarkerGen=this.mapMarkerGen.bind(this)
的操作,对mapMarkerGen
的每次调用现在都将具有正确的this
引用,无论它在哪里调用。我添加了this.mapMarkerGen=this.mapMarkerGen.bind(this)到构造函数,没有更改任何代码,但当前没有更改任何内容。在调用mapMarkerGen的函数中,我设置了_this=this;然后使用\u this=this
调用{this.mapMarkerGen()},如果原始this
引用不正确,则该调用将无效。因为代码不见了,我说不出有什么问题。我建议您检查getMarkerInfo
definition或call中是否存在任何打字错误。并查看在您定义\u this=this
并从那里开始工作的地方是否可以访问它。顺便说一句,为什么您要将\u这个定义为mapMarkerGen
的全局而不是局部?可能是您在某个地方覆盖了\u this
的值并破坏了内容。