update
Overview
update provides the ability to update values in the global state. update is
the dual of observe. observe enables reading live values
from state, update allows changing values in state.
API
update.<path> returns an object with following properties:
.set(value: any, params?: object)to replace the value of<path>in state, or create it if it doesn't exist yet.paramsis an optional object argument, the keys of which set the params..merge(value: any, params?: object)accepts an object, and merge it with existing object value of<path>in state.remove(params?: object)removes the<path>from state.push(value: any, params?: object)if the value at the given<path>is an array, then the value will be appened to the array.pop(params?: object)if the value at the given<path>is an array, then the last element will be removed
Example
If the state looks like:
{
"foo": {
"bar": "baz"
}
}
Operations to change the value of bar can be obtained by assigning
update.foo.bar in the header of a view. e.g
const MyComponent: producer = ({ bar = update.foo.bar }) => {
bar.set('qux');
...
}