@impact-react/reactive-context
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Observable contexts for React

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> }

Package Sidebar

Install

npm i @impact-react/reactive-context

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

84.3 kB

Total Files

8

Last publish

Collaborators

  • christianalfoni