You get HrStateWrapper instances inside the action handlers in makeHr.
The wrapper allows you to perform efficient, high level updates to the state object without mutation.
HrStateWrapper::constructorSignature: new HrStateWrapper(path: StatePath)
Creates a HrStateWrapper instance. This is typically done for you.
HrStateWrapper::invokeSignature: .invoke(fn: (s: HrStateWrapper) => mixed)
Utility for running a function without breaking the chain. Receives the
HrStateWrapper as an argument.
HrStateWrapper::idSignature: .id(id: string)
Returns a state wrapper for the specified id. You can then call methods
like .set on it.
s.id('some-id').set('some-value')
HrStateWrapper::listSignature: .list()
Returns a state wrapper for the list.
s.list().set(['a', 'b'])
HrStateWrapper::kvSignature: .kv(kvKey: string)
Returns a state wrapper for the specified key in the key/value pair.
s.kv('some-key').set('some-value')
HrStateWrapper::keySignature: .key(key: string)
Returns a state wrapper for the specified sub-key.
s.key('some-key').id('some-id').set('foo')
HrStateWrapper::queryRootSignature: .queryRoot()
Get an HrQuery object for the state.
HrStateWrapper::querySignature: .query()
HrStateWrapper::setSignature: .set(value: any)
Sets the currently focused item. See the examples for id/list/kv
HrStateWrapper::setIdsSignature: .setIds(pairs: Array<[string, any]>)
For each item in pairs, map the first item in the pair (the id) to the
second item (the value).
Often you’ll generate this with an array.map call.
s.setIdPairs(items.map(x => [x.id, x]))
s.key('some-key').setIdPairs(items.map(x => [x.id, x]))
HrStateWrapper::updateSignature: .update(updater: Function)
Update an item with the given id by passing it to the ‘updater’ function.
HrStateWrapper::updateInSignature: .updateIn(updatePath: Array<string | number>, updater: Function)
Update an item with the given id by passing it to the ‘updater’ function.
HrStateWrapper::setLoadingSignature: .setLoading()
Set the focused item to a loading state. Also captures the loading state.
s.id('some-id').setLoading()
HrStateWrapper::setLoadingDoneSignature: .setLoadingDone()
Set the focused item to a completed loading state.
s.id('some-id').setLoadingDone()
HrStateWrapper::setErrorSignature: .setError(error: ?any)
Set the focused item to an error state. Pass null to clear the error state.
s.id('some-id').setError({ message: 'Oops' })
HrStateWrapper::setMetaSignature: .setMeta(metaKey: string, metaValue: any)
Set custom metadata for the current item.
s.id('some-id').setMeta('isNew', true)
HrStateWrapper::pushSignature: .push(...items: Array<any>)
Push an item to the list.
Throws if not in list mode.
HrStateWrapper::unshiftSignature: .unshift(...items: Array<any>)
Adds an item to the start of the list.
Throws if not in list mode.
HrStateWrapper::popSignature: .pop(count: number = 1)
Removes items from the end of the list. The count defaults to 1.
Throws if not in list mode.
HrStateWrapper::shiftSignature: .shift(count: number = 1)
Removes items from the start of the list. The count defaults to 1.
Throws if not in list mode.
HrStateWrapper::optimisticSignature: .optimistic(id: ?string)
Set this operation to be optimistic, which can be rolled back on future state wrappers.
s.optimistic('token').id('some-id').set(value);
// in the future
s.optimistic('token').rollback()
HrStateWrapper::clearOptimisticSignature: .clearOptimistic(id: ?string)
Clear optimistic updates for the given key. Usually good to do this when your operation succeeds.
HrStateWrapper::rollbackSignature: .rollback()
Rolls back a previous optimistic update.
s.optimistic('token').rollback()
HrStateWrapper::getStateSignature: .getState()
Compute the state by applying all update operations. Mostly for internal use.
HrStateWrapper::rootSignature: .root()
Get the root HrStateWrapper instance.
HrStateWrapper::_pushOpSignature: ._pushOp(op: t.OpType, data: any, overrides: $Shape<t.HrStateWrapperOp> = {})
Internal: adds an operation to the queue