@impact-react/reactive-context
v1.0.0
Published
Reactive contexts for React
Downloads
5
Maintainers
Readme
Why
Observable primitives are typically organised in a global context. With impact-react-store
you can organise any observable primitives and the related state management with React contexts.
Read more about impact-react and how to use nested stores.
Install
npm install impact-react-store
Configure store
To use observable primitives with the React context you need to configure the props so that they also become observable. When React reconciles the props passed into the store might change, which you will be able to observe.
mobx
import { configureStore } from "impact-react-store";
import { observable } from "mobx";
export const createStore = configureStore((prop) => {
const box = observable.box(prop);
return {
get() {
return box.get();
},
set(newProp) {
box.set(newProp);
},
};
});
There is no change to the typing.
jotai
import { configureStore } from "impact-react-store";
import { atom } from "jotai";
export const createStore = configureStore((prop) => {
const value = atom(prop);
const getter = atom((get) => get(value));
return {
get() {
return getter;
},
set(newProp) {
value.set(newProp);
},
};
});
Type the props as type Props = { foo: Atom<string> }
Legend
import { configureStore } from "impact-react-store";
import { observable, computed } from "@legendapp/state";
export const createStore = configureStore((prop) => {
const value = observable(prop);
const getter = computed(() => value.get());
return {
get() {
return getter;
},
set(newProp) {
value.set(newProp);
},
};
});
Type the props as type Props = { foo: ObservableComputed<string> }