# context-state

> Like unstated-next, but Pro

Latest version **4.0.1** (published 2025-03-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install context-state
pnpm add context-state
yarn add context-state
bun add context-state
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.1 |
| Published | 2025-03-11 |
| First published | 2023-02-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 58.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Minko |
| Maintainers | hemengke- |
| Keywords | react, state management, hox, use-context-selector, unstated-next |

## Links

- npm: https://www.npmjs.com/package/context-state
- Repository: https://github.com/hemengke1997/context-state
- Issues: https://github.com/hemengke1997/context-state/issues
- npm.io page: https://npm.io/package/context-state

## Dependencies (1)

- [use-sync-external-store](https://npm.io/package/use-sync-external-store.md) ^1.4.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 4.0.1 (latest) — 2025-03-11
- 4.0.0 — 2025-03-11
- 3.2.0 — 2024-12-02
- 3.1.3 — 2024-11-26
- 3.1.2 — 2024-09-10
- 3.1.1 — 2024-08-08
- 3.1.0 — 2024-08-08
- 3.0.0 — 2024-04-26
- 2.4.0 — 2024-04-26
- 2.3.1 — 2024-04-23
- 2.3.0 — 2024-03-05
- 2.2.2 — 2024-03-05
- 2.2.1 — 2024-03-04
- 2.2.0 — 2024-02-29
- 2.1.0 — 2023-12-22
- … 2 more at https://npm.io/package/context-state/versions

## README

# context-state

> React state management solution with Context
> Like unstated-next, but Pro

[中文文档](./README.zh.md)

## Install

```bash
npm i context-state
```

## Migrate

If you are using v3, please refer to [here](./docs/migrate-v4.md) to upgrade to v4

## Advantage

React Context and useContext have some performance issues. When the context changes, all components that use the context will re-render. With `context-state`, developers don't need to worry about context penetration issues.

## Example

```tsx
import React from 'react';
import { createStore } from 'context-state';

function useCounter() {
  const [count, setCount] = React.useState(0);
  const increment = () => setCount((c) => c + 1);

  return {
    count,
    increment,
  };
}

const CounterStore = createStore(useCounter);

function CounterDisplay() {
  const { count, increment } = CounterStore.usePicker(['count', 'increment']);

  return (
    <div>
      {count}
      <button
        type='button'
        onClick={increment}
      >
        ADD
      </button>
    </div>
  );
}

function App() {
  return (
    <CounterStore.Provider>
      <CounterDisplay />
    </CounterStore.Provider>
  );
}

render(<App />, document.getElementById('root'));
```


## API

### `createContainer(useHook, options)`

```tsx
import { createStore, useMemoizedFn } from 'context-state';

function useCustomHook(props: {
  initialValue: string;
}) {
  const [value, setInput] = useState(props.initialValue);
  const onChange = useMemoizedFn((e) => setValue(e.currentTarget.value));
  return {
    value,
    onChange,
  };
}

const Store = createStore(useCustomHook, {
  // middlewares, used to listen to store changes
  middlewares: [{
    onInit: () => {},
    onChange: () => {}
  }]
});
// Store === { Provider, useStore }
```

If `useCustomHook` has parameters, they can be passed through `Store.Provider`.

### `<Container.Provider>`

```tsx
const Container = createContainer(useCustomHook);
function ParentComponent({ children }) {
  return <Container.Provider>{children}</Container.Provider>;
}
```

### `<Store.Provider>`

```tsx
const Store = createStore(useCustomHook);
function ParentComponent({ children }) {
  return <Store.Provider initialValue={'value'}>{children}</Store.Provider>;
}
```

### `Store.useStore()`

`useStore` is used to get the return value from the Provider.

`useStore` accepts 3 types of parameters:

1. Array. Only returns the values corresponding to the keys.
  
```tsx
function App() {
  const { count } = Store.useStore(['count']);
}
```

2. Function. Returns the return value of the function.

```tsx
function App() {
  const count = Store.useStore((store) => store.count);
}
```

3. No parameters. Returns all values.

```tsx
function App() {
  const store = Store.useStore();
}
```

**For best performance, it is recommended to use 1 and 2, which can avoid unnecessary rendering.**

## Inspiration

[unstated-next](https://github.com/jamiebuilds/unstated-next)

[use-context-selector](https://github.com/dai-shi/use-context-selector)

[hox](https://github.com/umijs/hox)

---
_Source: https://npm.io/package/context-state · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
