Answer the question
In order to leave comments, you need to log in
How to edit object in store redux?
Hello.
Such a reducer:
export default function person(state = {name:'Joe', age:30}, action) {
switch (action.type) {
case 'CHANGE_PERSON_NAME':
return state = {...state, name:action.name}
default:
return state
}
}
changePersonName = ( )=>{
this.props.dispatch( { type: 'CHANGE_PERSON_NAME', name: 'George' } );
}
export default function person(state = {name:'Joe', age:30}, action) {
switch (action.type) {
case 'CHANGE_PERSON_NAME':
return state.name = action.name // вот здесь не срабатывает!! см. аналогичную строку выше
default:
return state
}
}
Answer the question
In order to leave comments, you need to log in
It is connected with the mechanism of work of redux itself and the concept of immutability embedded in it. Checking through deepEqual that an object has changed is an overly expensive operation that must be performed per reducer. You can read more here
I'm not talking about that. that immutability, pure function, etc. - this concept is clear. the question is how to edit the elements of an object.
Here is an example from the documentation:
1.
export default function todos(state = [], action) {
switch (action.type) {
case 'ADD_TODO':
return state.concat([action.text])
default:
return state
}
}
export default function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
const store = createStore(reducer)
console.log(store.getState())
// {
// counter: 0,
// todos: []
// obj: {
// key1: prop1,
// key2: prop2,
// key3: prop3,
// key4: prop4,
// ...
// }
// }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question