函数不在React Native中的render内部触发

时间:2018-06-07 20:25:10

标签: reactjs react-native

我仍然是React Native的新手,但我仍然坚持这个简单的问题。

我在ComponentWillMount生命周期方法中进行API调用,将API数据添加到组件状态(不使用Redux),然后映射状态以呈现数据。

当我将我的功能代码放在“渲染”部分时,它会正确输出,但是当我将代码放入函数并在渲染区域内调用函数时,不会显示任何内容。

以下相关代码:

// function that is not executing
renderAlbums() {
    this.state.albums.map(album=><Text>{album.title}</Text>)
}

render() {

    return (
        <View>
            //this code works correctly
            /*this.state.albums.map(album=><Text>{album.title}</Text>)*/               

           //nothing is being displayed 
           {this.renderAlbums()}
        </View>
    )
}  

非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:1)

你需要在renderAlbums函数中返回map函数的结果,如下所示:

renderAlbums() {
    return this.state.albums.map(album=><Text>{album.title}</Text>)
}
相关问题