Skip to main content

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: next on the last step and prev on the first do nothing, and goTo moves to the nearest valid step (NaN is ignored).
  • If count shrinks, for example when an optional step is removed, the step moves to the new last step instead of pointing past the end. A count below 1 means a single step.
  • next, prev and goTo keep 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>
</>
);
}