Handle async logic with handleStep
mainYou can attach asynchronous logic to a step using handleStep. This is useful for performing API calls or validations before proceeding to the next step.
- If the handler is
asyncor returns aPromise,isLoadingwill becometruewhile the promise is pending. - If the handler throws an error, the wizard will stay on the current step and rethrow the error, allowing you to handle it with a
try-catchblock. - If the handler succeeds, the wizard automatically proceeds to the next step.
const Step1 = () => {
const { handleStep } = useWizard();
// Async function
handleStep(async () => {
await fetch(...);
});
// OR
// Return promise
handleStep(() => {
return fetch(...);
});
// ...
}