Skip to content
usefy

useStep

State & data

A React hook for multi-step navigation (wizards, forms, carousels)

Install

$ npm install @usefy/use-step

Quick start

useStep.tsx
import { useStep } from "@usefy/use-step";

function Wizard() {
  const [
    step,
    { goToNextStep, goToPrevStep, canGoToNextStep, canGoToPrevStep },
  ] = useStep(4);

  return (
    <div>
      {step === 0 && <InfoForm />}
      {step === 1 && <ConfirmForm />}
      {step === 2 && <PaymentForm />}
      {step === 3 && <CompleteMessage />}

      <button onClick={goToPrevStep} disabled={!canGoToPrevStep}>
        Back
      </button>
      <button onClick={goToNextStep} disabled={!canGoToNextStep}>
        Next
      </button>
    </div>
  );
}

API reference

useStep(count, initialStep?)

Returns a tuple of the current step index and a stable controls object.

Parameters

ParameterTypeDefaultDescription
countnumberTotal number of steps. Valid indices are 0 .. count - 1 (e.g. useStep(4)0,1,2,3). Values < 1 or non-finite are treated as a single step
initialStepnumber0Starting step index (floored and clamped to the valid range)

Returns [currentStep, controls]

ItemTypeDescription
currentStepnumberThe current 0-based step index
controlsUseStepControlsStable navigation controls (see below)

Controls

ControlSignatureDescription
goToNextStep() => voidAdvance one step. No-op on the last step
goToPrevStep() => voidGo back one step. No-op on the first step
canGoToNextStepbooleanWhether there is a next step
canGoToPrevStepbooleanWhether there is a previous step
setStep(step: number | (current: number) => number) => voidJump to a step (value or updater). Floored and clamped to [0, count - 1]
reset() => voidReset back to the initial step

Go deeper

This page is the quick reference. For every example, prop, and edge case, read the full README — or open Storybook to change props live.

More in state & data