Answer the question
In order to leave comments, you need to log in
How to save all values in 1 state when clicking?
I caught a blunt, let's say there is a state = {listItems: null} and when I click, I take some object and throw it there, on the next click I have to take the previous object + the new one and write it to the same state. At the end, the listItem should come out of the new objects, but how to do it. Here is my wrong implementation:
state = {
listItems: null,
}
addItem = (itemId) => {
const newItem = this.props.itemsList.filter(
item => itemId === item.id
)
this.setState(state => ({
listItems: [state.listItems, newItem[0]]
}))
}
Answer the question
In order to leave comments, you need to log in
state = {
listItems: []
}
addItem = (itemId) => {
const newItem = this.props.itemsList.find(item => itemId === item.id);
newItem && this.setState(state => ({
...state,
listItems: [...state.listItems, newItem]
}))
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question