useFormSteps
Manages step navigation — determines the next step based on conditions, maintains a history stack for back navigation, and tracks the current step.
You probably don’t need this directly.
useFormStatecalls it internally. Use it when building custom step navigation outside the standard<Form />component.
Signature
import { useFormSteps } from '@saastro/forms';
const { currentStepId, stepHistory, nextStep, prevStep, isLastStep, getNextStep } = useFormSteps(
steps,
initialStepId,
);
Parameters
| Parameter | Type | Description |
|---|---|---|
steps | Steps | Steps configuration map (stepId to step config) |
initialStepId | string | The step to start on |
Return Value
| Property | Type | Description |
|---|---|---|
currentStepId | string | The active step ID |
stepHistory | string[] | Stack of previously visited step IDs |
nextStep | (values) => boolean | Navigate forward. Returns true if successful |
prevStep | () => boolean | Navigate back. Returns true if successful |
isLastStep | (values) => boolean | Whether the current step is the last one |
getNextStep | (values) => string | undefined | Resolve the next step ID without navigating |
How Navigation Works
Forward (nextStep)
- Evaluates the current step’s
nextconditions againstvalues - First matching condition’s
targetbecomes the next step - If no conditions match, uses
defaultNext - Pushes
currentStepIdontostepHistoryand updatescurrentStepId - Returns
falseif no valid next step exists
Backward (prevStep)
- Pops the last entry from
stepHistory - Sets it as the new
currentStepId - Returns
falseif the history stack is empty
History Stack
The history records the exact path the user took, not all available steps. This means “Back” always returns to where the user came from, even with conditional routing:
Step A → Step C → Step E (history: ['A', 'C'])
^ Back goes to C, then A
Example
import { useFormSteps } from '@saastro/forms';
function StepWizard({ steps, initialStep, values }) {
const { currentStepId, stepHistory, nextStep, prevStep, isLastStep } = useFormSteps(
steps,
initialStep,
);
return (
<div>
<p>Current: {currentStepId}</p>
<p>History: {stepHistory.join(' → ')}</p>
{stepHistory.length > 0 && <button onClick={prevStep}>Back</button>}
{isLastStep(values) ? (
<button onClick={() => console.log('Submit!')}>Submit</button>
) : (
<button onClick={() => nextStep(values)}>Next</button>
)}
</div>
);
}
Related
- Multi-Step Forms — Full guide on conditional routing and step configuration
- useFormStepsInfo — Step metadata for building progress UI