I was hoping to be able to update a state object from this:
this.state = {
items: {},
}
to this:
this.state = {
items: {
2020-01-01: [
{'text': 'Some text', 'start_time': 'Some time'},
{'text': 'Some text', 'start_time': 'Some time'}],
2020-01-02: [
{'text': 'Some text', 'start_time': 'Some time'},
{'text': 'Some text', 'start_time': 'Some time'}]
}
}
by using spread operator:
//response from the API
response.items.map((item, index) => {
this.setState(prevState => ({
items: {
...prevState.items,
[item.time]: [
...prevState.items[item.time],
{
'text': item.summary,
'start_time': item.start_time
}]
}
}))
});
but that generates a TypeError:
TypeError: Invalid attempt to spread non-iterable instance
The purpose of updating the state object in such way is to render this component with data pulled from an API. But I can't seem to get it right.
Array.reduce, and once object built, you set the state to avoid inconsistencies ?