Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  6] [ 5]  / answers: 1 / hits: 23293  / 7 Years ago, thu, january 26, 2017, 12:00:00

I have a state in a reducer that looks like this:



// The current source/selection
const selection = {
timespan: -3660,
customTimespan: false,
pathIds: [''],
source: undefined,
direction: 0,
appClassIds: []
};


What I want now is to update multiple properties (timespan and customTimeSpan), something like this (but this doesn't work):



{ ...state, 
{
timespan: action.timespan.value,
customTimespan: action.timespan.value
}
};


How can I update multiple properties of a state?


More From » reactjs

 Answers
21

You need to remove the extra object closure from there



{ ...state, 
timespan: action.timespan.value,
customTimespan: action.timespan.value
}


should work fine



If you wanted to do it in vanilla JS you could do this:



Object.assign({}, state, { timespan: action.timespan.value, customTimespan: action.timespan.value})


I think the spread operator is much cleaner and should go that route if you have access to it.


[#59186] Wednesday, January 25, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
susanajamiep

Total Points: 466
Total Questions: 113
Total Answers: 108

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
susanajamiep questions
Sun, Jun 12, 22, 00:00, 2 Years ago
Mon, Mar 7, 22, 00:00, 2 Years ago
Wed, Jun 10, 20, 00:00, 4 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
;