I am consuming resources from an API and storing them in the states, specifically in ListaItems: [] but when I update the states with the data coming from the api they do not pass me the values... returning an empty array in ListaItems: [] I am working with context api, I feel that in this.setState I am not passing the route correctly to feed the data to the array
export class AppProvider extends React.Component {
constructor(props) {
super(props);
this.state = {
store: {
listadoItems: [],
carroItems: []
}
,
action: {
addItem: this.addItem,
showModal: this.showModal
}
}
}
componentDidMount() {
this.getInfo();
}
getInfo = () => {
fetch('https://jsonplaceholder.typicode.com/users')
.then(resp => {
console.log(resp.status);
return resp.json();
})
.then(data => {
console.log('recibe: ', data);
this.setState({
listadoItems: data
})
console.log('listado: ', this.state.store.listadoItems);
})
}
In your constructor you are assigning the property
listadoItems
inside the propertystore
and the latter to thestate
. In the use ofsetState
you are omitting the propertystore
, therefore you are adding the property to the component's statelistadoItems
. As a result, you should be left with a state with the following structure:You should change the line where you set the state to: