Join our Discord Community

useStep

Custom React hook that manages and navigates between steps in a multi-step process. Perfect for wizards, onboarding flows, and step-by-step forms with Next.js integration and TypeScript support.

Loading component...

Installation

npx shadcn@latest add https://www.shadcn.io/registry/use-step.json
npx shadcn@latest add https://www.shadcn.io/registry/use-step.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/use-step.json
bunx shadcn@latest add https://www.shadcn.io/registry/use-step.json

Features

  • Step navigation with automatic boundary checking and validation
  • Convenient helpers including goToNextStep, goToPrevStep, and reset methods
  • Boundary validation preventing invalid step transitions with boolean flags
  • Type safety with comprehensive TypeScript definitions and error handling
  • Flexible step setting supporting both direct values and function updates
  • Memory efficient with proper callback memoization to prevent unnecessary re-renders

Examples

Advanced Step Controls

Loading component...

Demonstrates a complete multi-step workflow with progress indicators, conditional navigation, and step validation.

Use Cases

This free open source React hook works well for:

  • Onboarding flows - Guide users through multi-step registration or setup processes
  • Wizards and forms - Step-by-step data collection with Next.js and TypeScript
  • Checkout processes - Navigate through cart, shipping, payment, and confirmation steps
  • Tutorial systems - Interactive tutorials and guided tours built with React
  • Progress workflows - Track user progress through complex multi-stage processes
  • Installation guides - Step-by-step software or configuration setup flows

API Reference

useStep

ParameterTypeDescription
maxStepnumberThe maximum step in the process (must be >= 1)

Return Array

Returns a tuple: [currentStep, actions]

IndexTypeDescription
0numberCurrent step number (1-based indexing)
1UseStepActionsObject containing navigation methods and state flags

UseStepActions Object

PropertyTypeDescription
canGoToNextStepbooleanWhether navigation to next step is possible
canGoToPrevStepbooleanWhether navigation to previous step is possible
goToNextStep() => voidMemoized function to go to the next step
goToPrevStep() => voidMemoized function to go to the previous step
reset() => voidMemoized function to reset to step 1
setStepDispatch<SetStateAction<number>>Direct step setter with validation

Usage Patterns

Basic Usage

const [
  currentStep,
  { goToNextStep, goToPrevStep, canGoToNextStep, canGoToPrevStep },
] = useStep(5);

// Navigate forward
if (canGoToNextStep) {
  goToNextStep();
}

// Navigate backward
if (canGoToPrevStep) {
  goToPrevStep();
}

With Step Setting

const [currentStep, { setStep, reset }] = useStep(10);

// Jump to specific step
setStep(5);

// Using function syntax
setStep((prev) => prev + 2);

// Reset to beginning
reset();

Wizard Implementation

const [currentStep, helpers] = useStep(4);
const { goToNextStep, goToPrevStep, canGoToNextStep, canGoToPrevStep, reset } =
  helpers;

const handleNext = () => {
  if (validateCurrentStep()) {
    goToNextStep();
  }
};

return (
  <div>
    <WizardStep step={currentStep} />
    <button onClick={goToPrevStep} disabled={!canGoToPrevStep}>
      Previous
    </button>
    <button onClick={handleNext} disabled={!canGoToNextStep}>
      Next
    </button>
    <button onClick={reset}>Start Over</button>
  </div>
);

Implementation Notes

  • Hook starts at step 1 and validates that maxStep is a positive number
  • All navigation functions respect boundaries and will not exceed maxStep or go below 1
  • Step validation throws descriptive errors for invalid step values in setStep
  • All helper functions are memoized with useCallback for optimal performance
  • Compatible with React 16.8+ and follows hooks rules and conventions
  • Works seamlessly with TypeScript for full type safety and IntelliSense
  • Helper methods have stable references across re-renders for performance
  • Step setting supports both direct values and functional updates like useState