RxState
Simple opinionated state management library based on RxJS
Installation
npm install --save rxstate rxjs
Quick start
Example code for creating a store with status and typeahead fetching action is shown below:
;;;; // create status actionconst status = ; // create action that fetches typeahead suggestions from serverconst getTypeahead = ;const typeahead$ = getTypeahead$; // create an array of action streams for storeconst streams = status$ typeahead$;// create storeconst store = ; // other place in code:// subscribe for state updatesstore; // other place in code:// trigger action;
Things to keep in mind
- Rxstate has RxJS as peer dependency - don't forget to install it as well!
- Store will always return last value to new subscribers.
- By default, the state is updated using spread operator on new and old state (e.g.
{...oldState, ...newState}
). You can change that by passingcombinator
parameter during store creation, e.g.:
// create combinator that always returns new stateconst combinator = data;// create storeconst store = ;
- Status action and stream can be created using
createStatus
function. By default it'll write status as{status: 'statusText'}
. Key can be changed by passing the string parameter to the function, e.g.:
// create status with custom keyconst status = ;// state will be updated with {customStatus: 'statusText'}
- Stores have
.clear()
method that accepts new initial state as an optional argument and dispatches new action with either provided or default state as value. If you use default combinator logic - this will reset your state to initial one.