# hooks/usePersistedState (zidane-ui)

Import: `import { … } from 'zidane-ui'`. Source: [ui/src/hooks/usePersistedState.ts](https://github.com/Tahul/zidane/blob/main/ui/src/hooks/usePersistedState.ts).

## Functions

### `oneOf`

Function.

`parse` for a closed string union: accepts only the listed members.

```ts
function oneOf<T extends string>(members: readonly T[]): (raw: string) => T | undefined
```

**Type parameters**

- `T` (`extends string`)

**Parameters**

- `members` (`readonly T[]`): The accepted values.

**Returns** `(raw: string) => T | undefined`

Source: [ui/src/hooks/usePersistedState.ts:88](https://github.com/Tahul/zidane/blob/main/ui/src/hooks/usePersistedState.ts#L88)

### `parseBoolean`

Function.

`parse` for a boolean stored as `'true'` / `'false'`.

```ts
function parseBoolean(raw: string): boolean | undefined
```

**Parameters**

- `raw` (`string`): The stored string.

**Returns** `boolean | undefined`

Source: [ui/src/hooks/usePersistedState.ts:97](https://github.com/Tahul/zidane/blob/main/ui/src/hooks/usePersistedState.ts#L97)

### `readPersisted`

Function.

The value stored under `key`, parsed; `undefined` when absent, unrecognised or when storage throws.

```ts
function readPersisted<T>(
  key: string,
  parse: (raw: string) => T | undefined,
): T | undefined
```

**Type parameters**

- `T`

**Parameters**

- `key` (`string`): The `localStorage` key.
- `parse` (`(raw: string) => T | undefined`): Validates the stored string; `undefined` for anything it doesn't recognise.

**Returns** `T | undefined`

Source: [ui/src/hooks/usePersistedState.ts:24](https://github.com/Tahul/zidane/blob/main/ui/src/hooks/usePersistedState.ts#L24)

### `usePersistedState`

Function.

`useState` kept in `localStorage` under `key`: hydrates from a valid stored value, writes on every set.

```ts
function usePersistedState<T>(
  key: string,
  fallback: T,
  parse: (raw: string) => T | undefined,
  serialize?: (value: T) => string | null,
): [T, Dispatch<SetStateAction<T>>]
```

**Type parameters**

- `T`

**Parameters**

- `key` (`string`): The `localStorage` key.
- `fallback` (`T`): The value when nothing valid is stored.
- `parse` (`(raw: string) => T | undefined`): Validates the stored string.
- `serialize?` (`(value: T) => string | null`): The stored form of a value; `null` removes the key (default `String`, `null` for nullish).

**Returns** `[T, Dispatch<SetStateAction<T>>]`

Source: [ui/src/hooks/usePersistedState.ts:60](https://github.com/Tahul/zidane/blob/main/ui/src/hooks/usePersistedState.ts#L60)

### `writePersisted`

Function.

Store `value` under `key`; `null` / `undefined` removes it. Never throws.

```ts
function writePersisted(key: string, value: string | null | undefined): void
```

**Parameters**

- `key` (`string`): The `localStorage` key.
- `value` (`string | null | undefined`): The serialized value, or `null` / `undefined` to remove it.

Source: [ui/src/hooks/usePersistedState.ts:40](https://github.com/Tahul/zidane/blob/main/ui/src/hooks/usePersistedState.ts#L40)
