Skip to Content
HooksuseToggle

useToggle

useToggle is a boolean state hook for UI flags such as dialogs, feature switches, visibility, and opt-in settings.

Live Example

Feature toggle panel

This shows real UI usage: one boolean state drives status UI and multiple controls.

Feature flag

Beta checkout

Disabled

This is a real usage pattern: one boolean state drives a status badge, explanatory text, and small control helpers.

Import

import { useToggle } from "react-rsc-kit/client";

Signature

const [value, toggle, setValue, actions] = useToggle(initialValue?);

Parameters

NameTypeDefaultDescription
initialValueboolean | (() => boolean)falseInitial toggle state, with lazy initialization support.

Returns

ItemDescription
valueCurrent boolean state.
toggleFlips the value, or forces it when called with true or false.
setValueReact state setter for direct control.
actionsHelper object with setTrue, setFalse, and reset.

Usage

"use client"; import { useToggle } from "react-rsc-kit/client"; export function FeatureFlagControls() { const [enabled, toggle, , { setTrue, setFalse, reset }] = useToggle(false); return ( <div> <p>Enabled: {String(enabled)}</p> <button type="button" onClick={() => toggle()}> Toggle </button> <button type="button" onClick={setTrue}> Enable </button> <button type="button" onClick={setFalse}> Disable </button> <button type="button" onClick={reset}> Reset </button> </div> ); }

Notes

  • Works without an argument and defaults to false.
  • Keeps the tuple API simple while adding reusable helper actions.
  • Helper callbacks are stable across rerenders.
Last updated on