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.
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
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
Parameter | Type | Description |
---|---|---|
maxStep | number | The maximum step in the process (must be >= 1) |
Return Array
Returns a tuple: [currentStep, actions]
Index | Type | Description |
---|---|---|
0 | number | Current step number (1-based indexing) |
1 | UseStepActions | Object containing navigation methods and state flags |
UseStepActions Object
Property | Type | Description |
---|---|---|
canGoToNextStep | boolean | Whether navigation to next step is possible |
canGoToPrevStep | boolean | Whether navigation to previous step is possible |
goToNextStep | () => void | Memoized function to go to the next step |
goToPrevStep | () => void | Memoized function to go to the previous step |
reset | () => void | Memoized function to reset to step 1 |
setStep | Dispatch<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
useSessionStorage
Custom hook that uses the sessionStorage API to persist state across page reloads with serialization and cross-tab synchronization. Perfect for React applications requiring temporary state persistence with Next.js integration and TypeScript support.
useTernaryDarkMode
React hook for managing ternary dark mode (system, dark, light) with local storage support and system preference detection.