Skip to content

Instantly share code, notes, and snippets.

@sr-shifu
Last active March 20, 2024 22:13
Show Gist options
  • Save sr-shifu/b048502a02e548260e3c7885ff34afea to your computer and use it in GitHub Desktop.
Save sr-shifu/b048502a02e548260e3c7885ff34afea to your computer and use it in GitHub Desktop.
State machine wizard Context
const StateMachineWizardContext = createContext({});
export const StateMachineWizard = (/* props goe here */) => {
const contextValue = useMemo(
() => ({
errors,
setStepData,
setErrors,
navigateTo,
}),
[errors, setStepData, setErrors, navigateTo]
);
// ... in the modal body
<StateMachineWizardContext.Provider value={contextValue}>
<WizardSteps
wizardNavHistory={navHistory}
wizardDataByStep={wizardDataByStep}
curStepName={curStepName}
/>
</StateMachineWizardContext.Provider>
}
// use this hook on client's step component side
export const useStateMachine = () => {
// TODO: handle some common common logic
return React.useContext(StateMachineWizardContext);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment