# zustand

🐻 Bear necessities for state management in React

## Install

```bash
npm install zustand
```

Install these too only if you use what needs them: `@types/react`, `immer`, `react`, `use-sync-external-store`.

## Functions

### `useStore`

```ts
function useStore<S extends ReadonlyStoreApi<unknown>>(
  api: S,
): ExtractState<S>
function useStore<S extends ReadonlyStoreApi<unknown>, U>(
  api: S,
  selector: (state: ExtractState<S>) => U,
): U
```

## Constants

### `create`

```ts
const create: Create
```

### `createStore`

```ts
const createStore: CreateStore
```

## Interfaces

### `StoreApi`

```ts
interface StoreApi<T>
```

**Properties**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `setState` | `SetStateInternal<T>` |  |  |
| `getState` | `() => T` |  |  |
| `getInitialState` | `() => T` |  |  |
| `subscribe` | `(listener: (state: T, prevState: T) => void) => () => void` |  |  |

### `StoreMutators`

Also has every member of `StoreMutators`, listed on its own entry.

```ts
interface StoreMutators<S, A>
```

## Types

### `ExtractState`

```ts
type ExtractState<S> = S extends { getState: () => infer T } ? T : never
```

### `Mutate`

```ts
type Mutate<S, Ms> = number extends Ms['length' & keyof Ms]
  ? S
  : Ms extends []
    ? S
    : Ms extends [[infer Mi, infer Ma], ...infer Mrs]
      ? Mutate<StoreMutators<S, Ma>[Mi & StoreMutatorIdentifier], Mrs>
      : never
```

### `StateCreator`

```ts
type StateCreator<
  T,
  Mis extends [StoreMutatorIdentifier, unknown][] = [],
  Mos extends [StoreMutatorIdentifier, unknown][] = [],
  U = T,
> = ((
  setState: Get<Mutate<StoreApi<T>, Mis>, 'setState', never>,
  getState: Get<Mutate<StoreApi<T>, Mis>, 'getState', never>,
  store: Mutate<StoreApi<T>, Mis>,
) => U) & { $$storeMutators?: Mos }
```

**Properties**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `$$storeMutators?` | `Mos` |  |  |

### `StoreMutatorIdentifier`

Also has every member of `String`, listed on its own entry.

```ts
type StoreMutatorIdentifier = keyof StoreMutators<unknown, unknown>
```

### `UseBoundStore`

Also has every member of `StoreApi`, listed on its own entry.

```ts
type UseBoundStore<S extends ReadonlyStoreApi<unknown>> = {
  (): ExtractState<S>
  <U>(selector: (state: ExtractState<S>) => U): U
} & S
```
