I am having a hard time to figure this out.
I am using redux in my app and want to return a completely new object from my switch statement. Lets say we have an initial state and then when a componentWillMount we make an ajax request and we want to override the initial state with these new values. The keys of the object from the ajax request are the SAME as the initial state object. However, the values obviously changed. What is the most efficient and clean way to return a new javascript object?
const INITIAL_STATE = {
name: "",
email: "",
phoneNumber: "",
uid: "",
photoURL: "",
address: {
city: "",
country: "",
street: "",
details: ""
},
pastOffers: [],
memberSince: "",
gender: "",
birthday: "",
description: "",
worker: false
};
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case FETCH_USER_SUCCESS:
return { ...state, HOW DO I INSERT ACTION.PAYLOAD HERE };
default:
return state;
}
};
I was thinking of using lodash and doing a _.each() and then passing an object to the reducer with (prop, value) and then doing:
return { ...state, [action.payload.prop]: action.payload.value }
but that seems a little too much. Is there any simpler way?
{ ...state, action.payload }- this is an error. This -{ ...state, ...action.payload }spreads them both into a new object.