当我试图调用一个返回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>
);
}
但我不明白上面的代码是从哪里来的.但当我try 在我的render()
方法中创建一个变量,其中一个通过我的map()
方法时,一切都很好:
render() {
const c=this.state.albums.map(album => <Text>{ album.title }</Text>);
return (
<View>
{ c }
</View>
);
}
我想知道为什么它不起作用.当我在这里调用render renderAlbums()
方法<View>{ this.renderAlbums }</View>
时.
对我来说更奇怪的是,当我试着用括号来称呼renderAlbums()
,比如<View>{ this.renderAlbums() }</View>
时,它是有效的.