33

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?

Dan Prince
  • 29,491
  • 13
  • 89
  • 120
ffxsam
  • 26,428
  • 32
  • 94
  • 144

6 Answers6

58

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,
    },
};
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • @jcubic thanks! And sorry for being picky, but it's not an "operator". I misused the term, it's just a "spread syntax" (so I also corrected my answer in that regards) – zerkms Aug 22 '18 at 20:55
  • In the beginning of ECMAScript specification 9.0 https://www.ecma-international.org/ecma-262/9.0/index.html they say "rest parameter and spread operator", don't know why MDN docs say "syntax". – jcubic Aug 23 '18 at 13:47
  • @jcubic I think it's a mistake: from the language perspective it cannot be an operator, it's punctuation. I reported an issue about that, thanks! https://github.com/tc39/ecma262/issues/1295 – zerkms Aug 23 '18 at 20:04
46

Although it's possible to use the spread operator, there are lots of other ways to achieve the same result without even needing a future JS compiler for a non standardised feature. Here are some other options in no particular order.

Return a Literal

If you are sure that your state won't grow, then you can simply return the entire new state as a literal.

return {
  notificationBar: {
    open: true
  }
}

However, that's not often going to be appropriate because it's unlikely that your state will be this simple.


Combine Reducers

Redux gives you a utility method for combining several reducers that work on different parts of the state object. In this case, you'd create a notificationBar reducer that handled this object alone.

 createStore(combineReducers({
   notificationBar: function(state=initialNBarState, action) {
     switch (action.type) {
       case actions.LAYOUT_NOTIFICATIONBAR_OPEN:
         return Object.assign({}, state, { open: true });
   }
 });

This prevents you from having to worry about the top level of properties, so that you can avoid nesting calls to Object.assign.

If your state can logically broken down into clearly defined sections then this is probably the most idiomatic way to solve this problem.


Use Immutable Data

You can use an persistent data structures library to create data structures than can be modified to return a copy.

Mori

Mori is the result of compiling Clojure's data structures and functional API into JS.

import { hashMap, updateIn } from 'mori';

const initialState = hashMap(
  "notificationBar", hashMap(
    "open", false
  )
);

// ...

return updateIn(state, ['notificationBar', 'open'], true);

ImmutableJS

ImmutableJS is a more imperative approach to bringing the semantics of Hash Array Mapped Tries from Clojure's persistent data structures to Javascript.

import { Map } from 'immutable';

const initialState = Map({
  notificationBar: Map({
    open: true
  });
});

// ...

return state.setIn(['notificationBar', 'open'], true);

Alias Object.assign

You can create a friendlier version of Object.assign to write terser versions of the code above. In fact, it can be nearly as terse as the ... operator.

function $set(...objects) {
  return Object.assign({}, ...objects);
}

return $set(state, {
  notificationBar: $set(state.notificationBar, {
    open: true,
  })
});

Use Immutable Helpers

There are a number of libraries that also offer immutability helpers for making modifications to regular mutable objects.

react-addons-update

React has had a built in set of immutability helpers for a long time. They use a similar syntax to MongoDB queries.

import update from 'react-addons-update';

return update(state, {
  notificationBar: {
    open: { $set: true }
  }
});

dot-prop-immutable

This library allows you to use familiar dot paths to specify updates to (nested) properties.

import dotProp from 'dot-prop-immutable';

return dotProp.set(state, 'notificationBar.open', true);

update-in

This library is a wrapper around react-addons-update and provides a more functional syntax for updating (nested) properties.

Rather than passing a new value, you pass a function which takes the old value and returns a new one.

import updateIn from 'update-in';

return updateIn(state, ['notificationBar', 'open'], () => true);

immutable-path

For updating properties, this library is like a cross between dot-prop-immutable and update-in.

import path from 'immutable-path';

return path.map(state, 'notificationBar.open', () => true);
zeckdude
  • 15,877
  • 43
  • 139
  • 187
Dan Prince
  • 29,491
  • 13
  • 89
  • 120
  • 2
    And also various other libs similar to the React Helpers, such as dot-prop-immutable, update-in, immutable-path, and object-path-immutable. – markerikson Feb 24 '16 at 05:00
  • you could use `Boolean.bind(0,1)` instead of an arrow/anon for those last couple... – dandavis Feb 26 '16 at 10:30
  • Sure, but that's a pretty confusing way of expressing a function that always returns `true`. Not only is it easier to read and understand, but bound functions are also less performant and harder to debug. – Dan Prince Feb 26 '16 at 10:41
  • @DanPrince BTW, what do you mean when you say rest/spread is not standardized? I thought it was officially part of ES6. – ffxsam Feb 27 '16 at 18:43
  • 2
    @ffxsam It is for arrays, not for objects. – Dan Prince Feb 27 '16 at 21:47
  • Ah! You're right, the MDN reference for rest/spread has no examples with objects. I'll look into Immutable then. – ffxsam Feb 27 '16 at 21:49
  • 1
    There's an error in your dot-prop-immutable example.. should be `return dotProp.set(state, 'notificationBar.open', true);` - missing .set. Sorry, SO won't let me edit the answer if less than 6 chars. – James Gentes Sep 25 '16 at 19:54
  • can I use dot-prop-immutable without the path param ? (second param) – Julien Malige Dec 15 '17 at 17:02
  • Based on this list, i'd say dotProp is just mind-blowingly simple and powerful. Thanks for the suggestion. If it helps anyone, my (non functional) implentation is at https://jsfiddle.net/dapdm3wL/ and is taken to merely be an example of how simple this can be to use. I'd say that using the .path system can be as simple as structuring a path via an array and traversing that way as seen in my fiddle. – Hybrid web dev Jan 28 '18 at 08:21
  • Combine Reducers one of the best way to handle complexity – 44kksharma May 01 '19 at 11:06
6

You can use Lenses.

import { set, makeLenses } from '@DrBoolean/lenses'

const L = makeLenses(['notificationBar', 'open']);
const notificationBarOpen = compose(L.notificationBar, L.open)
const setNotificationBarOpenTrue = set(notificationBarOpen, true)

const a = { notificationBar: { open: false } }
const b = setNotificationBarOpenTrue(a) 
// `a` is left unchanged and `b` is `{ notificationBar: { open: true } }`

You can think about Lenses as compositional property access/update.

Some good resources about Lenses:

If you are ok with reading lisps, I would also recommend taking look at this excellent introduction to lenses from racket docs. Finally, if you want to go deeper and are ok with reading haskell you can watch: Lenses - compositional data access and manipulation.

soundyogi
  • 369
  • 3
  • 15
Safareli
  • 842
  • 10
  • 18
1

If you're using Immutable.js, you can look under Nested Structures topic some functions that may help you, I personally use mergeDeep:

prevState.mergeDeep({ userInfo: {
  username: action.payload.username,
} }),
Ricardo Mutti
  • 2,639
  • 2
  • 19
  • 20
0

In addition to what've been said earlier, here is a functional way with Ramda:

import { assocPath } from 'ramda';

const o1 = { a: { b: { c: 1 }, bb: { cc: 22 } } };
const o2 = assocPath(['a', 'b', 'c'])(42)(o1);
console.log(o1 !== o2, o1.a !== o2.a); // new copies of "changed" objects
console.log(o1.a.bb === o2.a.bb); // deep unchanged properties are copied by reference
aeldar
  • 21
  • 2
0

All advice here are great and valid, but I would like to offer another solution. The problem which appears here is definitely a common pattern, so I think it is much better just to write your own interface for such updates and stick with it inside reducers, and use one function to update deeply inside all your reducers.

For example, I have created a library, where I tried to address this issue the next way: I get the type of the module (so-called "tile"), function to perform operations (both async and sync) and desired nesting based on passed params. So, for your case it will be something like:

import { createSyncTile } from 'redux-tiles';
const uiTile = createSyncTile({
  type: ['ui', 'elements'],
  fn: ({ params }) => params,
  // type will be `notificationBar`
  nesting: ({ type }) => [type],
});

And that's it -- it will be update correctly on arbitrary nesting. Also, tile provides selectors, so you don't have to worry personally where precisely data is located, you can just use them. So, I don't want to say it is the best solution, but the idea is pretty simple -- don't be afraid to write your own implementation, and then just use factory to solve this issue.

Bloomca
  • 1,784
  • 13
  • 17