@yogjin/react-global-state
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

React-global-state · NPM Version GitHub license

react-global-state is simple global state management library for React 18.

Installation

The Recoil package lives in npm

To install the latest stable version, run the following command:

npm i @yogjin/react-global-state

Or if you're using yarn:

yarn add @yogjin/react-global-state

How to use

Fundamentally, it's very similar to the concept of Recoil.

globalState is similar to atom

useGlobalState is similar to useRecoilState

Make globalState

import { globalState } from '@yogjin/react-global-state';

export const countState = globalState([1]);

Use useGlobalState

React Component

import { countState } from './globalStates';
import { useGlobalState } from '@yogjin/react-global-state';

export default function Component() {
  const [counts, setCounts] = useGlobalState(countState);
  ...
}

Non-React Code

Even outside of React components, you can use useSetGlobalState to set the globalState.

Likewise, if there's a change in the state, any component subscribed to that globalState will be re-rendered.

import { countState } from "./globalStates";
import { useSetGlobalState } from '@yogjin/react-global-state';

const setCountState = useSetGlobalState(countState);

const add100ToCountState = () => {
    setCountState((prevCount) => [...prevCount, 100]);
}

License

react-global-state is MIT licensedhttps://github.com/yogjin/react-global-state/blob/main/LICENSE.

Package Sidebar

Install

npm i @yogjin/react-global-state

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

11.8 kB

Total Files

15

Last publish

Collaborators

  • yogjin