@jkempema/replace-state
v1.0.3
Published
function that replaces a section of state
Downloads
1
Readme
replace-state
function that allows replacing sections of an object graph
Usage
Replace using a path of properties
import replaceState from 'replace-state';
const state = {
p1: {
p2: 'some-value'
}
};
const newState = replaceState( state, 'p1.p2', 'new-value' );
Replace using an array of properties
import replaceState from 'replace-state';
const state = {
p1: {
p2: 'some-value'
}
};
const newState = replaceState( state, [ 'p1', 'p2' ], 'new-value' );
Replace when the value is an array
import replaceState from 'replace-state';
const state = {
p1: {
p2: [ {
id: 1,
value: 'some-value'
}, {
id: 2,
value: 'some-other-value'
} ]
}
};
const newState = replaceState( state, [ 'p1', ( item ) => item.id === 1 ], 'new-value' );
Replace with a function
import replaceState from 'replace-state';
const state = {
p1: {
p2: 'some-value'
}
};
const newState = replaceState( state, 'p1.p2', ( p2 ) => 'new-value' );