【发布时间】:2018-08-01 05:41:40
【问题描述】:
当我尝试调用返回 map 方法结果的方法时,我收到以下错误 Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render 但我不知道为什么。这是我的代码:
renderAlbums() {
return this.state.albums.map(album => <Text>{ album.title }</Text>);
}
render() {
return (
<View>
{ this.renderAlbums }
</View>
);
}
但是我上面的代码我看不出问题出在哪里。但是,当我尝试在我的 render() 方法中创建一个变量并传递我的 map() 方法时,一切正常:
render() {
const c=this.state.albums.map(album => <Text>{ album.title }</Text>);
return (
<View>
{ c }
</View>
);
}
我想了解它为什么不起作用。当我在这里调用渲染renderAlbums() 方法时<View>{ this.renderAlbums }</View>。
而且对我来说更奇怪的是,当我尝试像这样用括号调用 renderAlbums() 时,它可以工作。
【问题讨论】:
-
您没有执行该功能,您只是提供参考。
this.renderAlbums()将执行它 -
也谢谢你,你帮助我更好地理解了
标签: reactjs react-native