re-state
API Reference

setReStateInitialValue

setReStateInitialValue is an advanced API for changing one key's reset baseline. It affects what resetReState restores later; it does not replace the key's current value.

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

export const useTheme = createReState<'light' | 'dark'>('theme', 'light');
const dispatchTheme = createReStateDispatch<'light' | 'dark'>('theme');

setReStateInitialValue('theme', 'dark');
dispatchTheme('light');
resetReState(); // theme is now "dark"

For a key that already exists, changing the baseline does not change the current value or notify subscribers. For a new key, the computed baseline also becomes its current value.

Function values

At runtime, a function passed as value is treated as an initializer rather than stored directly. It receives the current value, or undefined for a new key, and its result becomes the new baseline:

const computeResetCount = (previous: number | undefined): number => previous ?? 0;

setReStateInitialValue('count', computeResetCount);

The initializer runs immediately. A later call to resetReState restores its result without running the function again.

For most state modules, the valueOfReset option from createReStateMethods is easier to keep next to the state definition.

API

function setReStateInitialValue<T>(key: string, value: T): void;
ParameterDescription
keyState key whose reset baseline should change
valueDirect value, or an initializer function as described above

On this page