. Advertisement .
..3..
. Advertisement .
..4..
I am trying to write a javascript that returns an invalid answer. I don’t know where the incorrect command is in the “functions are not valid as a react child.”
My detail is:
renderAlbums() {
return this.state.albums.map(album => <Text>{ album.title }</Text>);
}
render() {
return (
<View>
{ this.renderAlbums }
</View>
);
}
render() {
const c=this.state.albums.map(album => <Text>{ album.title }</Text>);
return (
<View>
{ c }
</View>
);
}
and I end up with the warning message:
Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render
Pls, suggest the best answer to fix it.
The cause: The cause is that React expects
React Elements
to render it.this.renderAlbums
is a function reference in the current script that does not return any React Element. Because of not the react element, but the function itself. As a result, React is unable to renderthis.renderAlbums
.The solution: You should change this code
To:
this.renderAlbums
refers only to the function,this.renderAlbums()
executes it and represents its return value.