javascript - How do I get my reducer to update one property of an object in state? - Stack Overflow

For example, in props I have an object called 'user' that is set to: user = { id: 1, name: ji

For example, in props I have an object called 'user' that is set to:

user = { id: 1, 
         name: jim, 
         email: [email protected], 
         entries: 4,
         joined: 07-16-2018
       }

I want to have the UPDATE_USER_ENTRIES action dispatch updates only to the user.entries using the payload

What I have for my current reducer function is this:

export const loadUserData = (state = initialStateUser, action = {}) => {
    switch(action.type) {
        case LOAD_USER:
            return Object.assign({}, state, { 
                user: { 
                    id: action.payload.id, 
                    name: action.payload.name, 
                    email: action.payload.email, 
                    entries: action.payload.entries, 
                    joined: action.payload.joined 
                }
            });
        case UPDATE_USER_ENTRIES:
            return {...state, **// NEED HELP HERE** };
        default:
            return state;
    }
}

What is the proper way of writing the return in order to return a new user object with just the entries properties updated? Thanks!

For example, in props I have an object called 'user' that is set to:

user = { id: 1, 
         name: jim, 
         email: [email protected], 
         entries: 4,
         joined: 07-16-2018
       }

I want to have the UPDATE_USER_ENTRIES action dispatch updates only to the user.entries using the payload

What I have for my current reducer function is this:

export const loadUserData = (state = initialStateUser, action = {}) => {
    switch(action.type) {
        case LOAD_USER:
            return Object.assign({}, state, { 
                user: { 
                    id: action.payload.id, 
                    name: action.payload.name, 
                    email: action.payload.email, 
                    entries: action.payload.entries, 
                    joined: action.payload.joined 
                }
            });
        case UPDATE_USER_ENTRIES:
            return {...state, **// NEED HELP HERE** };
        default:
            return state;
    }
}

What is the proper way of writing the return in order to return a new user object with just the entries properties updated? Thanks!

Share Improve this question asked Jul 17, 2018 at 1:37 kayqkayq 1333 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You can use spread operator like so:

If your state is like:

state = {
     foo: "bar",
     user: {
         id: 1, 
         name: "jim", 
         email: "[email protected]", 
         entries: 4,
         joined: "07-16-2018",
    }
}

the code below, spread the state, do not touch foo, spread state's user, update the entries property.

case UPDATE_USER_ENTRIES:
    return {...state, user: {...state.user, entries: action.payload } };

If your state looks like:

user: {
         id: 1, 
         name: "jim", 
         email: "[email protected]", 
         entries: 4,
         joined: "07-16-2018",
    }

the above code spreads the state, which is user only, then updates the entries property.

case UPDATE_USER_ENTRIES:
    return {...state, entries: action.payload };

Your payload is holding just one value: userEntries hence it is only equal to userEntries. So in your reducer you need to use it according to this: action.payload Your payload is not an object which holds a userEntries value.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745169259a4614822.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信