impact-react-store
TypeScript icon, indicating that this package has built-in type declarations

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

/impact-react-store/

    Package Sidebar

    Install

    npm i impact-react-store

    Weekly Downloads

    7

    Version

    1.2.0

    License

    MIT

    Unpacked Size

    78.7 kB

    Total Files

    8

    Last publish

    Collaborators

    • christianalfoni