如何制作一个React Native组件数组?

时间:2018-03-27 16:15:59

标签: javascript reactjs react-native react-table

我尝试在我的视图中使用react-native-table-componentdocs),但我遇到了一个问题,因为我希望我的列有图像组件。我按照使用按钮的文档中的示例进行操作,并且具有以下内容:

    console.log("following pic arr: " + this.state.followingPics);
    var following_pics;
    if(this.state.followingPics.length === 0) {
        following_pics = [];
    }
    else {
        //this.state.followingPics is an array of picture urls
        JSON.parse(this.state.followingPics).forEach((pic, i) => {
            following_pics.push(makeImage(pic));
        })
    }

    var tableArr = [following_pics, following];

    return (
        <View style={shared.basicView}>
            <TextInput
                autoCorrect={false}
                style={{height: 40}}
                placeholder="Follow a user..."
                onSubmitEditing={(input) => this.addFollower(input.nativeEvent.text)}
            />
            <Table style={styles.table}
                   borderStyle={{borderWidth: 0.5, borderColor: '#c8e1ff'}}>
                <Row data={tableHead}
                     style={styles.head} textStyle={styles.text}
                     flexArr={[1]}
                />
                <Cols data={tableArr}
                      textStyle={styles.text}
                      heightArr={[50, 50, 50, 50, 50, 50, 50, 50]}
                      flexArr={[1, 2, 1, 2]}
                />
            </Table>
        </View>
    );

但我一直收到错误undefined is not an object (evaluating following_pics.push)。有没有什么方法可以将这些图片添加为指定的数据数组?

1 个答案:

答案 0 :(得分:0)

您在else区块中执行的操作是尝试将push元素转换为未定义的变量:

console.log("following pic arr: " + this.state.followingPics);
var following_pics;  // this will be undefined in else block
if(this.state.followingPics.length === 0) {
    following_pics = [];
}
else {
    //this.state.followingPics is an array of picture urls
    JSON.parse(this.state.followingPics).forEach((pic, i) => {
        following_pics.push(makeImage(pic));
    })
}

我不明白为什么你需要这个块,为什么不只是:

console.log("following pic arr: " + this.state.followingPics);
var following_pics = [];
//this.state.followingPics is an array of picture urls
JSON.parse(this.state.followingPics).forEach((pic, i) => {
   following_pics.push(makeImage(pic));
})