1.2.0 • Published 11 months ago

impact-react-store v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

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

1.2.0

11 months ago

1.1.0

11 months ago

1.0.0

11 months ago