Answer the question
In order to leave comments, you need to log in
How does state and setState work internally?
There is a React code:
state = {
activeItem: 0,
test: 1
}
onSelectItem = i => {
this.setState({
activeItem: i,
})
}
Answer the question
In order to leave comments, you need to log in
setState works in the following way:
it takes as input a function that returns a new state or a new state. Since state is an object then it does the following:
// ... - spread operator
state = {...state, ...newState} // since newState comes after state then it will overwrite all old values with new ones or add new
https://learn.javascript.ru/rest-parameters-spread...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question