Answer the question
In order to leave comments, you need to log in
How to refer to nested object?
Hello.
I am writing a react redux application, I want to use something like this state
const initialState = {
user: {
response: {},
error: null,
isFetching: false
},
friends: {
response: {
count: 0,
items: []
},
error: null,
isFetching: false
}
};
switch (action.type) {
case types.GET_USER_LOADED:
return {...state, user.response: action.payload};
.......
const initialState = {
user: {},
userIsFetching: false,
userError: null,
friends:{
count:0,
items:[]
},
friendsIsFetching: false,
friendsError: null
};
const api = (state = initialState, action)=>{
switch (action.type){
case types.GET_USER_LOADED:
return{...state, user:action.payload};
........
Answer the question
In order to leave comments, you need to log in
You need to sequentially recreate all nested objects:
return {
...state,
user: {
...state.user,
response: { ...action.payload },
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question