我想创建一个带有图像的滚动条,链接在数组 LINKS 中。这是在类里面完成的。
如果我只使用 uri:"http://something.jpg"
制作一张图片,它会完美地工作,但这里在我添加的行上出现“undefined is not an object”错误ImageButton 的导航器。
(我尝试同时提供按钮导航器或直接的 onPress 功能,但它们都不起作用)。
{
LINKS.map(function(val){
return <ImageButton
uri={val}
navigator={this.props.navigator}
onPress={() => {
console.log(this.props.uri);
this.props.navigator.push({
id:'image',
uri:this.props.uri,
sceneConfig: Navigator.SceneConfigs.FloatFromRight,
});
}}
/>
})
}
也许我对 array.map() 有一些不理解的地方。
最佳答案
在 map 函数中绑定(bind) this
的范围:
LINKS.map(function(val){
}.bind(this));
关于bind
的更多信息:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind
顺便说一下,如果您像在 onPress 回调中那样在这里使用粗箭头,您就可以避免这个问题,因为 this
的范围是自动设置的。
关于javascript - 如何在 React Native 中创建组件映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31584956/