Api Methods
createReState

createReState

Creates a reusable hook for a specific global state. Unlike useReState where you pass the key every time, createReState binds the key once and returns a ready-to-use hook.

Why use createReState?

  • Encapsulation: Define state key and type in one place
  • Reusability: Export a named hook that's easy to import
  • Type safety: TypeScript types are defined once, not repeated
  • Cleaner imports: Components import useMuted() instead of useReState('muted', false)

Basic Usage

// states/muted.ts
import { createReState } from '@raulpesilva/re-state';
 
export const useMuted = createReState<boolean>('muted', false);
// components/MuteButton.tsx
import { useMuted } from '../states/muted';
 
function MuteButton() {
  const [muted, setMuted] = useMuted();
 
  return (
    <button onClick={() => setMuted(!muted)}>
      {muted ? 'Unmute' : 'Mute'}
    </button>
  );
}

Examples

Sharing State Across Components

// states/volume.ts
import { createReState } from '@raulpesilva/re-state';
 
export const useVolume = createReState<number>('volume', 50);
// components/VolumeSlider.tsx
import { useVolume } from '../states/volume';
 
function VolumeSlider() {
  const [volume, setVolume] = useVolume();
 
  return (
    <input
      type="range"
      min="0"
      max="100"
      value={volume}
      onChange={e => setVolume(Number(e.target.value))}
    />
  );
}
// components/VolumeDisplay.tsx
import { useVolume } from '../states/volume';
 
function VolumeDisplay() {
  const [volume] = useVolume();
  return <p>Volume: {volume}%</p>;
}

Both components share the same volume state.

Complex State Types

// states/user.ts
import { createReState } from '@raulpesilva/re-state';
 
interface User {
  id: string;
  name: string;
  email: string;
  avatar: string;
}
 
const initialUser: User = {
  id: '',
  name: '',
  email: '',
  avatar: '',
};
 
export const useUser = createReState<User>('user', initialUser);
// components/UserCard.tsx
import { useUser } from '../states/user';
 
function UserCard() {
  const [user, setUser] = useUser();
 
  const updateName = (name: string) => {
    setUser(prev => ({ ...prev, name }));
  };
 
  return (
    <div>
      <img src={user.avatar} alt={user.name} />
      <input
        value={user.name}
        onChange={e => updateName(e.target.value)}
      />
    </div>
  );
}

Multiple States in One File

// states/audio.ts
import { createReState } from '@raulpesilva/re-state';
 
export const useVolume = createReState<number>('volume', 50);
export const useMuted = createReState<boolean>('muted', false);
export const usePlaying = createReState<boolean>('playing', false);
// components/AudioControls.tsx
import { useVolume, useMuted, usePlaying } from '../states/audio';
 
function AudioControls() {
  const [volume, setVolume] = useVolume();
  const [muted, setMuted] = useMuted();
  const [playing, setPlaying] = usePlaying();
 
  return (
    <div>
      <button onClick={() => setPlaying(!playing)}>
        {playing ? 'Pause' : 'Play'}
      </button>
      <button onClick={() => setMuted(!muted)}>
        {muted ? 'Unmute' : 'Mute'}
      </button>
      <input
        type="range"
        value={volume}
        onChange={e => setVolume(Number(e.target.value))}
        disabled={muted}
      />
    </div>
  );
}

API Reference

function createReState<S>(
  key: string,
  initialValue?: S
): () => [S, (value: S | ((prev: S) => S)) => void]

Parameters

ParameterTypeRequiredDescription
keystringYesUnique identifier for the state
initialValueSNoInitial value of the state

Returns

A hook function that returns a tuple:

IndexTypeDescription
[0]SCurrent state value
[1](value: S | ((prev: S) => S)) => voidFunction to update the state

Comparison

FeatureuseReStatecreateReState
Key passed atCall siteDefinition
Best forQuick prototypingReusable state modules
Type defined atEach usageOnce
Import styleuseReState('key', value)useMyState()

When to Use

ScenarioRecommendation
Single state, need just the hookcreateReState
Need dispatch/get/reset utilitiescreateReStateMethods
Quick inline stateuseReState
Full state management solutioncreateReStateMethods

For most production use cases, consider createReStateMethods which gives you all utilities (useX, useXSelect, dispatchX, getX, resetX) in one call.