I am using fetch method to make API calls on the server. First, I do a request to have an array of objects (
[{"id": 1, "name": "a", "userid": "12"},{"id": 2, "name": "b", "userid": "22"},{"id": 3, "name": "c", "userid": "23"}].
After I need one value of this object to give more information ( in this case I need to userid to have the information of the user).
For that, I am doing multiple requests to show all at the moment. But doing multiple requests is giving me an error.
Objects are not valid as a React child (found: object with keys {_45, _81, _65, _54}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of
View
And this is my code. The second fetch is in renderImage function. I put as async but I know that probably is wrong.
class Dashboard extends Component{
constructor(props){
super(props)
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2
}),
loaded: false,
datos: '',
}
}
componentDidMount(){
this.fetchData();
}
fetchData(){
const REQUEST_URL = "XXXXXXX";
fetch(REQUEST_URL)
.then((response) => response.json()) //la convertimos a json
.then ((responseData) =>{
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData),
})
})
}
renderLoadingView(){
return(
<View>
<Text>Cargando...</Text>
</View>
)
}
async renderImage(userid){
const REQUEST_URL = "XXXXXXX" + userid;
const response = await fetch(REQUEST_URL);
const json = await response.json();
this.setState({loaded: true})
return (<Thumbnail source={{uri: json.imageUrl}} />)
}
renderReceta(receta){
return(
<Card>
<CardItem>
<TouchableOpacity onPress={()=> this.onUser(receta)}>
{this.renderImage(receta.user_id)}
</TouchableOpacity>
<Body>
<Text>{receta.Titulo}</Text>
<Text>{receta.Username}</Text>
</Body>
</CardItem>
</Card>
)
}
render(){
if(!this.state.loaded){
return this.renderLoadingView();
}
else{
return(
<Container>
<Header><Title>H</Title></Header>
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderReceta.bind(this)}
/>
</Container>
)
}
}
}
Objects are not valid as a React childmeans you are directly trying to render an object, which you cannot do. Check all of your rendered items. I get the sneaking suspicion that it's inrenderReceta