re-state
API Reference

createGetReState

Need the latest value in an event handler or utility? createGetReState gives you a getter for one key. It reads the value when you call it and never subscribes to changes.

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

type Session = {
  userId: string;
};

export const useSession = createReState<Session | null>('session', null);
export const getSession = createGetReState<Session | null>('session');
import { getSession } from '../states/session';

export function hasSession(): boolean {
  return getSession() !== null;
}

The factory does not create or initialize the key. If nothing initializes it first, the getter returns undefined at runtime even when its generic type describes a different value.

API

function createGetReState<S>(key: string): () => S;

key identifies the value to read. The getter always reads the latest value; it does not keep an earlier snapshot.

Use createReStateDispatch to update the same key, or createReStateMethods to generate both helpers together.

On this page