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

1.0.0-rc.2 • Public • Published

react-internal-store

Share state between your components without any magic ✋.

NPM version NPM downloads Test Coverage Coverage Status License

Usage

Basic

import { useState } from 'react';
import { createStore } from 'react-internal-store';

const MyModel = () => {
  const [count, setCount] = useState(0);

  const increment = useCallback(() => {
    setCount((prev) => prev + 1);
  }, []);

  return [count, increment];
};

const Store = createStore({ MyModel });

function MyApp() {
  return (
    <Store>
      <MyPage />
    </Store>
  );
}

function MyPage() {
  const [count, increment] = Store.useMyModel();

  return (
    <button onClick={increment} type="button">
      You've clicked {count} times.
    </button>
  );
}

With Selector

import { useState } from 'react';
import { createStore, defineModel } from 'react-internal-store';

const MyModel = defineModel(() => {
  const [count, setCount] = useState(0);

  const increment = useCallback(() => {
    setCount((prev) => prev + 1);
  }, []);

  return [{ count }, increment];
});

const Store = createStore({ MyModel });

function MyApp() {
  return (
    <Store>
      <MyPage />
    </Store>
  );
}

function MyPage() {
  const [count, increment] = Store.useMyModel((state) => {
    return state.count;
  });

  return (
    <button onClick={increment} type="button">
      You've clicked {count} times.
    </button>
  );
}

Readme

Keywords

none

Package Sidebar

Install

npm i react-internal-store

Weekly Downloads

0

Version

1.0.0-rc.2

License

MIT

Unpacked Size

47.1 kB

Total Files

43

Last publish

Collaborators

  • imhele