A lightweight alternative for RX and Redux, that utilises react hooks and dramatically reduces the amount of boiler plate code for the typical React application.
npm install --save reactive-properties yarn add reactive-properties
Property is just a variable that can be observed.
To get the current value use the
get method. To subscribe to future updates, use the
subscribe method. Subscribe returns a function which can be called to unsubscribe from updates.
For developer convenience all properties have a
pipe method. It can be used to apply operators to a property.
property.pipewithEffect,mapx * 2,
Simplest property implementation. You provide the default value and can call the
set method to update the value.
console.logstate.get // prints 5state.set42console.logstate.get // prints 42state.subscribeconsole.log'Update:', state.getstate.set99 // "Update: 99" is printed to the console
Sends update notifications to all subscribers when the new value is set.
Used to combine multiple properties into one.
b.set5 // sum is now 7a.set6 // sum is now 11
Updates every time when one of the source properties update.
Works similarly to
Array.map. Creates a new property which will always have values that are obtained by mapping the original property's values with the provided function.
Provides a way to add custom imperative logic when something starts observing the property.
Works similarly to
useEffect. Can be usefull to start and stop background tasks when user visits a specific page, for example.
Calls provided callback for each value that a property has. Including the current one.
Converts the property to a promise that resolves when the provided predicate returns true. Promise resolves with the first value that matched the predicate.
Stops updates when property values don't change. Example:
state.forEachconsole.log // prints 0state.set0 // nothing printedstate.set1 // prints 1state.set1 // nothing printed