useStep
function useStep(count: number, options?: StepOptions): Steps;
interface StepOptions {
initial?: number; // the step to start at, default 0
}
interface Steps {
readonly step: number; // counted from 0
readonly next: () => void;
readonly prev: () => void;
readonly goTo: (step: number) => void;
readonly isFirst: boolean;
readonly isLast: boolean;
}
Tracks which of count steps is shown, for wizards, onboarding tours and carousels.
- The step always stays in range:
nexton the last step andprevon the first do nothing, andgoTomoves to the nearest valid step (NaNis ignored). - If
countshrinks, for example when an optional step is removed, the step moves to the new last step instead of pointing past the end. Acountbelow 1 means a single step. next,prevandgoTokeep their identity.
Live demo
Step 1 of 3: Cart
Example
import { useStep } from "react-honey-hooks";
const pages = [<Account />, <Profile />, <Confirm />];
function SignUpWizard() {
const { step, next, prev, isFirst, isLast } = useStep(pages.length);
return (
<>
{pages[step]}
<button onClick={prev} disabled={isFirst}>
Back
</button>
<button onClick={next} disabled={isLast}>
Next
</button>
</>
);
}