React observing
advancedSelector
advancedSelector
The
advancedSelector
is in preview right now. NOT recommended for production.
Overview
Returns a function that returns a read-only IObservable or writeable IObservable.
A advancedSelector
is a powerful pattern that is similar to a selector, but allows you to pass parameters to the get
and set
callbacks of a selector.
The advancedSelector()
utility returns a function which can be called with user-defined parameters and returns a selector
.
Each unique parameter value will return the same memoized selector instance.
Definition
/** * Build a read only advanced selector state */export function advancedSelector<T, P extends TSerializableParam>(options: TReadWriteSelectorOptionsWithParams<T, P>): TReadWriteSelectorStateWithParams<T, P>;/** * Build a read and writable advanced selector state */export function advancedSelector<T, P extends TSerializableParam>(options: TReadOnlySelectorOptionsWithParams<T, P>): TReadOnlySelectorStateWithParams<T, P>;
Example
1const myNumberObservable = observe(2);23const myMultipliedObservable = advancedSelector({4 get: (multiplier) => ({get}) => {5 return get(myNumberObservable) * multiplier;6 },78 // optional set9 set: (multiplier) => ({set}, newValue) => {10 set(myNumberObservable, newValue / multiplier);11 },12});1314function MyComponent() {15 // defaults to 216 const number = useObserverValue(myNumberObservable);1718 // defaults to 20019 const multipliedNumber = useObserverValue(myMultipliedObservable(100));2021 return <div>...</div>;22}