mergeState change for nested Structure in React and Redux
setState
is often nasty when you unfortunately have nested structure in your React/Redux state.
This package makes setState
easy with the nested Structure.
Install
Just like other packages
npm i --save mergestate
The s
is lowercased due to my mistake and I cannot change the it. Sorry for the inconvinience.
Functions
mergeState(prevState, diff)
This function return an new object with similar structure ofprevState
and with changes according todiff
.prevState
is not changed in the function. This function searches changes byenumerable
entries ofdiff
deepSlowEqual(state1, state2, [...comparers])
This function recursively compare two objects on entries withenumerable
property.[...comparers]
is an array of functionf(x,y)
for additional rules in comparing two nodes that returntrue
false
orundefined
. The comparers are called recursively when comparing each nodes in the trees. If the comparer returntrue
orfalse
, thedeepSlowEqual
will stop comparing and returntrue
orfalse
; when the comparer returnundefined
, thedeepSlowEqual
will continue....comparers
could be used for ignoring certain types in comparing.
Usage as Example
This package search for changes recursively by the enumerable
entries in the diff
.
Assign a value in a nested Object; and Appending
;; let prevState = oo: a: b: 1 b: c: 1 c: d: 1 ;let diff = oo: a: b: 100 c: 99 ;let state = ; ; // prevState Unchanged; // merge State by enumerable entries of diff
Assign a value in a nested Array; and Appending a value in a nested Array
;; let prevState = aa: 1 1 1 1 1 1;let diff = aa: Object;let state = ; ; // prevState Unchanged; // merge State by enumerable entries of diff
Assign a value in a array nested in an object
;; let prevState = oa: a: 1 1 b: 1 1 c: 1 1 ;let diff = oa: a: Object ;let state = ; ; // prevState Unchanged; // merge State by enumerable entries of diff