store.setState
@@SPLICE Tag Usage
Sample:store.setState({
stateKey0: { // where `state.stateKey0` is an array
'@@SPLICE': [ // performs array splice on `state.stateKey0`.
-/+fromIndex,
deleteCount, // integer >= 0
...newInserts?
]
}
});
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { SPLICE_TAG } from '@webkrafters/react-observable-context'; // SPLICE_TAG = "@@SPLICE"
const state = {
a: { b: [{ x: 7, y: 8, z: 9 }, { x: 17, y: 18, z: 19 }] },
j: 10,
q: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
};
store.setState({ a: { [ SPLICE_TAG ]: [ 0, 1 ] } }); // assigning a '@@SPLICE' command to a non-array property has no effect.
/* removes state.a.b[0]; leaving state.a.b = [{ x: 17, y: 18, z: 19 }] */
store.setState({ a: { b: { [ SPLICE_TAG ]: [ 0, 1 ] } } }) // or store.setState({ a: { b: { [ SPLICE_TAG ]: [ -2, -1 ] } } });
/* replaces state.q[4] - [7] with 2 items; leaving state.q = [ 1, 2, 3, 4, 33, 88, 9 ] */
store.setState({ a: { q: { [ SPLICE_TAG ]: [ 4, 4, 33, 88 ] } } }) // or store.setState({ a: { q: { [ SPLICE_TAG ]: [ -5, 4, 33, 88 ] } } });