re-state
API Reference

createReStateDispatch

Use createReStateDispatch when an action, event handler, utility, or service needs to update shared state outside a React hook.

import { createReState, createReStateDispatch } from '@raulpesilva/re-state';

export const useCounter = createReState<number>('counter', 0);
export const dispatchCounter = createReStateDispatch<number>('counter');

export const increment = (): void => {
  dispatchCounter((previous) => previous + 1);
};

The dispatcher accepts a replacement value or an updater function:

dispatchCounter(10);
dispatchCounter((previous) => previous + 1);

If the key does not exist, the first dispatch creates it and also establishes its reset baseline. An updater for a new key receives undefined at runtime, so initialize the key first when the update depends on a previous value.

API

function createReStateDispatch<S>(key: string): (value: S | ((previous: S) => S) | undefined) => void;

key identifies the value to update. The returned dispatcher does not read or subscribe to state.

Use createGetReState for a matching getter, or createReStateMethods to create the full named API in one call.

On this page