0

I can successfully do the following which allows me to build an object of profiles with userid's as the key - I like this structure as it helps with other parts of the application

function profilesMembersResponseConcat(members, newMember) {
    members[newMember.userid] = newMember
    return  members
}

[C.PROFILES_MEMBERS_RESPONSE]: (state, { payload }) => ({
    ...state,
    profilesMembersResponse: profilesMembersResponseConcat(state.profilesMembersResponse, payload)
}),

Question: Is there a way however to do this without the need for a function. I've tried below but I get an error message: "Arrow function should not return assignment."

[C.PROFILES_MEMBERS_RESPONSE]: (state, { payload }) => ({
    ...state,
    profilesMembersResponse: state.profilesMembersResponse[payload.userid] = payload
}),
2
  • 1
    Why not using the function? It seems cleaner and also what you're doing on the second snippet is mutating the current state which is not a good practice. Commented Dec 1, 2019 at 1:36
  • good call on the mutating state... I was just interested to see if I could do this without a separate function... Commented Dec 1, 2019 at 1:51

1 Answer 1

1

Both of your solutions are mutating the object. You'll want to change your assignment to something more like:

[C.PROFILES_MEMBERS_RESPONSE]: (state, { payload }) => ({
  ...state,
  profilesMembersResponse: ({...state.profilesMembersResponse, [payload.userid]:payload})
}),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.