如何在React native中创建组件映射

How to Create a map of components in React native

本文关键字:创建 创建组 组件 映射 native React      更新时间:2023-09-26

我想创建一个带有图像的滚动条,链接在数组links中。这在类中完成。

如果我只用uri:"http://something.jpg"制作一个图像,它可以完美地工作,但这里有一个"未定义的不是对象"的错误,我将导航器添加到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函数中绑定this的作用域:

LINKS.map(function(val){
}.bind(this));

bind的更多信息:

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind

顺便说一句,如果你在这里使用一个胖箭头,就像你为你的onPress回调,你已经避免了这个问题,因为this的范围是自动设置的。