# hooks/keyed-store (zidane-ui)

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

## Functions

### `createKeyedStore`

Function.

A new empty [`KeyedStore`](https://zidane.si/docs/reference/ui-hooks-keyed-store.md#KeyedStore).

```ts
function createKeyedStore<V>(): KeyedStore<V>
```

**Type parameters**

- `V`

**Returns** `KeyedStore<V>`

Source: [ui/src/hooks/keyed-store.ts:22](https://github.com/Tahul/zidane/blob/main/ui/src/hooks/keyed-store.ts#L22)

### `useKeyedValue`

Function.

The value under `key` (`null` when unset or no key), re-rendering only when that key changes.

```ts
function useKeyedValue<V>(
  store: KeyedStore<V>,
  key: string | null | undefined,
): V | null
```

**Type parameters**

- `V`

**Parameters**

- `store` (`KeyedStore<V>`): The store to read.
- `key` (`string | null | undefined`): The key; `null` / `undefined` reads nothing.

**Returns** `V | null`

Source: [ui/src/hooks/keyed-store.ts:58](https://github.com/Tahul/zidane/blob/main/ui/src/hooks/keyed-store.ts#L58)

## Interfaces

### `KeyedStore`

Interface.

Values by key, each key with its own subscribers.

```ts
interface KeyedStore<V>
```

**Type parameters**

- `V`

**Properties**

- `delete` (`(key: string) => void`): Forget `key` (a deleted session): its readers see no value.
- `get` (`(key: string) => V | undefined`): The value under `key`, if any.
- `set` (`(key: string, value: V) => void`): Store `value` under `key` and notify that key's subscribers (nothing when it is the same value).
- `subscribe` (`(key: string, listener: () => void) => () => void`): Call `listener` whenever `key` changes; returns the unsubscribe.

Source: [ui/src/hooks/keyed-store.ts:10](https://github.com/Tahul/zidane/blob/main/ui/src/hooks/keyed-store.ts#L10)
