Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
194
rated 0 times [  199] [ 5]  / answers: 1 / hits: 29510  / 8 Years ago, wed, february 24, 2016, 12:00:00

Sometimes reducers get kind of messy:



const initialState = {
notificationBar: {
open: false,
},
};

export default function (state = initialState, action) {
switch (action.type) {
case actions.LAYOUT_NOTIFICATIONBAR_OPEN:
return Object.assign({}, state, {
// TODO: Find a cleaner way to do this!
notificationBar: Object.assign({}, state.notificationBar, {
open: true,
}),
});
default:
return state;
}
}


Is there a more terse way to do this?


More From » reactjs

 Answers
52

UPD: it's now a part of the ES2018



It might be slightly improved via a non-standardised yet properties spread syntax:



return {
...state,
notificationBar: {
...state.notificationBar,
open: true,
},
};

[#63192] Monday, February 22, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
payton

Total Points: 307
Total Questions: 96
Total Answers: 83

Location: Saint Vincent and the Grenadines
Member since Sat, Sep 11, 2021
3 Years ago
payton questions
Wed, Oct 27, 21, 00:00, 3 Years ago
Fri, Aug 20, 21, 00:00, 3 Years ago
Sun, Apr 11, 21, 00:00, 3 Years ago
Wed, Jun 26, 19, 00:00, 5 Years ago
;