Skip to content

Commit 49c65d5

Browse files
committed
Avoid stale goToStep callback
1 parent 60b8929 commit 49c65d5

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/wizard.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,25 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
2929
}
3030
});
3131

32-
const goToStep = React.useRef((stepIndex: number) => {
33-
if (stepIndex >= 0 && stepIndex < stepCount) {
34-
nextStepHandler.current = null;
35-
setActiveStep(stepIndex);
36-
} else {
37-
if (__DEV__) {
38-
logger.log(
39-
'warn',
40-
[
41-
`Invalid step index [${stepIndex}] passed to 'goToStep'. `,
42-
`Ensure the given stepIndex is not out of boundaries.`,
43-
].join(''),
44-
);
32+
const goToStep = React.useCallback(
33+
(stepIndex: number) => {
34+
if (stepIndex >= 0 && stepIndex < stepCount) {
35+
nextStepHandler.current = null;
36+
setActiveStep(stepIndex);
37+
} else {
38+
if (__DEV__) {
39+
logger.log(
40+
'warn',
41+
[
42+
`Invalid step index [${stepIndex}] passed to 'goToStep'. `,
43+
`Ensure the given stepIndex is not out of boundaries.`,
44+
].join(''),
45+
);
46+
}
4547
}
46-
}
47-
});
48+
},
49+
[stepCount],
50+
);
4851

4952
// Callback to attach the step handler
5053
const handleStep = React.useRef((handler: Handler) => {
@@ -78,9 +81,9 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
7881
stepCount,
7982
isFirstStep: !hasPreviousStep.current,
8083
isLastStep: !hasNextStep.current,
81-
goToStep: goToStep.current,
84+
goToStep,
8285
}),
83-
[activeStep, stepCount, isLoading],
86+
[isLoading, activeStep, stepCount, goToStep],
8487
);
8588

8689
const activeStepContent = React.useMemo(() => {

0 commit comments

Comments
 (0)