Design Converter
Education
Developer Advocate
Last updated on Oct 24, 2023
Last updated on Oct 10, 2023
The React Wizard is a powerful tool that provides a common UI design pattern for creating multi-step forms or processes. It's a component that allows you to break down complex tasks into smaller, manageable steps, guiding the user through each step one at a time. This is particularly useful in scenarios where you need to collect a lot of information from the user, such as in a lengthy registration form or a multi-step checkout process.
The React Wizard is built using the React use wizard, a custom hook that provides all the necessary functionalities to create a wizard in your React application. This includes handling the current step, navigating between steps, and managing the state of each step.
The basic wizard is the simplest form of a wizard. It consists of a series of steps, each represented by a child component. The wizard maintains the state of the current step and provides functions to navigate to the next or previous step.
1 const BasicWizard = () => { 2 const { step, nextStep, previousStep } = useWizard(); 3 4 return ( 5 <div> 6 {step === 1 && <Step1 />} 7 {step === 2 && <Step2 />} 8 {step === 3 && <Step3 />} 9 <button onClick={previousStep}>Previous</button> 10 <button onClick={nextStep}>Next</button> 11 </div> 12 ); 13 }; 14
In a React Wizard, each step is represented by a child component. These child components are responsible for rendering the content of each step. They can be as simple or as complex as needed, and can include anything from simple text and images to forms and interactive elements.
The wizard passes down props to each child component, including the current step, functions to navigate to the next and previous steps, and any additional data or functions needed for that step. This allows each child component to control its own state and behavior, while still being able to interact with the overall wizard.
The wizard form is a crucial component in a React Wizard. It is responsible for collecting and managing the data entered by the user in each step. This data is then passed back up to the wizard, which can use it to determine the flow of the wizard, validate the data, or store it for later use.
The wizard form is typically implemented as a controlled component, with the wizard maintaining the state of the form and passing down props to control the form's inputs. This allows the wizard to have full control over the form and its data, making it easier to manage and validate the data.
1 const WizardForm = ({ data, onChange }) => { 2 return ( 3 <form> 4 <input 5 type="text" 6 value={data.name} 7 onChange={e => onChange({ ...data, name: e.target.value })} 8 /> 9 <input 10 type="email" 11 value={data.email} 12 onChange={e => onChange({ ...data, email: e.target.value })} 13 /> 14 <button type="submit">Submit</button> 15 </form> 16 ); 17 }; 18
The sync steps animated wizard is a more advanced version of the basic wizard. It adds animations to the transitions between steps, providing a more engaging and visually pleasing user experience. This is achieved using an animation library, such as Framer Motion.
In a sync steps animated wizard, each step is wrapped in a framer motion const wrapper that handles the animations. The wizard then uses the sync steps integration function to synchronize the animations with the navigation between steps.
1 import { motion } from "framer-motion"; 2 3 const AnimatedStep = ({ children }) => { 4 return ( 5 <motion.div 6 initial={{ opacity: 0, x: -100 }} 7 animate={{ opacity: 1, x: 0 }} 8 exit={{ opacity: 0, x: 100 }} 9 > 10 {children} 11 </motion.div> 12 ); 13 }; 14 15 const SyncStepsAnimatedWizard = () => { 16 const { step, nextStep, previousStep } = useWizard(); 17 18 return ( 19 <div> 20 {step === 1 && <AnimatedStep><Step1 /></AnimatedStep>} 21 {step === 2 && <AnimatedStep><Step2 /></AnimatedStep>} 22 {step === 3 && <AnimatedStep><Step3 /></AnimatedStep>} 23 <button onClick={previousStep}>Previous</button> 24 <button onClick={nextStep}>Next</button> 25 </div> 26 ); 27 }; 28
The active step component is a key part of the React Wizard. It represents the current step that the user is on. The active step component is responsible for rendering the content of the current step and managing any state or behavior specific to that step.
The active step component receives props from the wizard, including the current step, functions to navigate to the next and previous steps, and any additional data or functions needed for that step. This allows the active step component to control its own state and behavior, while still being able to interact with the overall wizard.
1 const ActiveStep = ({ step, nextStep, previousStep }) => { 2 switch (step) { 3 case 1: 4 return <Step1 nextStep={nextStep} />; 5 case 2: 6 return <Step2 nextStep={nextStep} previousStep={previousStep} />; 7 case 3: 8 return <Step3 previousStep={previousStep} />; 9 default: 10 return null; 11 } 12 }; 13
In a React Wizard, navigation between steps is handled using the previous step index gotostep function. This function takes an index as an argument and navigates to the step at that index. This allows for flexible navigation, as you can jump to any step in the wizard, not just the next or previous step.
The previous step index gotostep function is particularly useful in scenarios where the flow of the wizard is not linear. For example, in a quiz wizard, the user might be able to skip certain steps based on their answers to previous questions.
1 const { step, gotoStep } = useWizard(); 2 3 return ( 4 <div> 5 {step === 1 && <Step1 gotoStep={gotoStep} />} 6 {step === 2 && <Step2 gotoStep={gotoStep} />} 7 {step === 3 && <Step3 gotoStep={gotoStep} />} 8 </div> 9 ); 10
One of the advantages of the React Wizard is that it promotes reusability. The same component can be used for multiple steps in the wizard, and the same step can be revisited multiple times.
This is particularly useful in scenarios where the steps in the wizard are similar or identical. For example, in a survey wizard, each step might consist of a question and a set of answer options. Instead of creating a separate component for each question, you can create a single Question component and reuse it for each step, passing in the question and options as props.
1 const Question = ({ question, options, answer, setAnswer }) => { 2 return ( 3 <div> 4 <h3>{question}</h3> 5 {options.map(option => ( 6 <button 7 key={option} 8 onClick={() => setAnswer(option)} 9 style={answer === option ? { backgroundColor: 'blue' } : {}} 10 > 11 {option} 12 </button> 13 ))} 14 </div> 15 ); 16 }; 17 18 const SurveyWizard = () => { 19 const { step, nextStep, previousStep } = useWizard(); 20 const [answer, setAnswer] = useState(null); 21 22 return ( 23 <div> 24 {step === 1 && ( 25 <Question 26 question="Question 1" 27 options={['Option 1', 'Option 2', 'Option 3']} 28 answer={answer} 29 setAnswer={setAnswer} 30 /> 31 )} 32 {step === 2 && ( 33 <Question 34 question="Question 2" 35 options={['Option 1', 'Option 2', 'Option 3']} 36 answer={answer} 37 setAnswer={setAnswer} 38 /> 39 )} 40 <button onClick={previousStep}>Previous</button> 41 <button onClick={nextStep}>Next</button> 42 </div> 43 ); 44 }; 45
In some cases, you might need to perform asynchronous operations in your wizard steps. For example, you might need to fetch data from an API, or save data to a database. The React Wizard supports this through the async step handler.
The async step handler is a function that is called when the user navigates to a step. This function can be either sync or async, and it can return a promise. If the function returns a promise, the wizard will wait for the promise to resolve before navigating to the step. This allows you to perform asynchronous operations and ensure that they complete before the user navigates to the step.
1 const { step, nextStep, previousStep } = useWizard(); 2 3 const handleStep = async (step) => { 4 if (step === 2) { 5 await fetch('/api/data'); 6 } 7 }; 8 9 return ( 10 <div> 11 {step === 1 && <Step1 nextStep={nextStep} />} 12 {step === 2 && <Step2 nextStep={nextStep} handleStep={handleStep} />} 13 {step === 3 && <Step3 previousStep={previousStep} />} 14 </div> 15 ); 16
In addition to the steps, a React Wizard can also include a header and a footer. The header typically displays the title of the wizard and the progress of the user through the steps. The footer typically includes navigation buttons to move to the next and previous steps, and a submit button to complete the wizard.
The header and footer components receive props from the wizard, including the current step, the total number of steps, and functions to navigate to the next and previous steps. This allows the header and footer to display dynamic content based on the state of the wizard, and to control the navigation of the wizard.
1 const Header = ({ step, stepCount }) => { 2 return ( 3 <div> 4 <h1>Wizard Title</h1> 5 <p>Step {step} of {stepCount}</p> 6 </div> 7 ); 8 }; 9 10 const Footer = ({ step, stepCount, nextStep, previousStep }) => { 11 return ( 12 <div> 13 {step > 1 && <button onClick={previousStep}>Previous</button>} 14 {step < stepCount && <button onClick={nextStep}>Next</button>} 15 {step === stepCount && <button>Submit</button>} 16 </div> 17 ); 18 }; 19 20 const Wizard = () => { 21 const { step, nextStep, previousStep, stepCount } = useWizard(); 22 23 return ( 24 <div> 25 <Header step={step} stepCount={stepCount} /> 26 {step === 1 && <Step1 />} 27 {step === 2 && <Step2 />} 28 {step === 3 && <Step3 />} 29 <Footer step={step} stepCount={stepCount} nextStep={nextStep} previousStep={previousStep} /> 30 </div> 31 ); 32 }; 33
The react use wizard is a custom hook that provides the core functionality of the React Wizard. It manages the state of the wizard, including the current step and any data associated with each step. It also provides functions to navigate to the next and previous steps, and to jump to a specific step.
The react use wizard hook can be used in any component that needs to implement a wizard. It provides a flexible and powerful way to manage the state and behavior of a wizard, and it can be customized to suit the specific needs of your application.
1 const { step, nextStep, previousStep, gotoStep, data, setData } = useWizard(); 2 3 return ( 4 <div> 5 {step === 1 && <Step1 data={data} setData={setData} nextStep={nextStep} />} 6 {step === 2 && <Step2 data={data} setData={setData} nextStep={nextStep} previousStep={previousStep} />} 7 {step === 3 && <Step3 data={data} setData={setData} previousStep={previousStep} />} 8 </div> 9 ); 10
In a React Wizard, each step is a building block that contributes to the overall process. Each step is represented by a component, which can be as simple or as complex as needed. Each step component receives props from the wizard, including the current step, functions to navigate to the next and previous steps, and any additional data or functions needed for that step.
The wizard maintains the state of the current step and provides functions to navigate to the next or previous step. This allows each step to control its own state and behavior, while still being able to interact with the overall wizard.
1 const Step1 = ({ nextStep }) => { 2 return ( 3 <div> 4 <h3>Step 1</h3> 5 <button onClick={nextStep}>Next</button> 6 </div> 7 ); 8 }; 9 10 const Step2 = ({ nextStep, previousStep }) => { 11 return ( 12 <div> 13 <h3>Step 2</h3> 14 <button onClick={previousStep}>Previous</button> 15 <button onClick={nextStep}>Next</button> 16 </div> 17 ); 18 }; 19 20 const Step3 = ({ previousStep }) => { 21 return ( 22 <div> 23 <h3>Step 3</h3> 24 <button onClick={previousStep}>Previous</button> 25 </div> 26 ); 27 }; 28
In a React Wizard, you might need to perform asynchronous operations, such as fetching data from an API or saving data to a database. The wizard supports this through the handler promise state.
The handler promise state is a state variable that holds the promise returned by the async step handler. The wizard uses this promise to determine when to navigate to the step. If the promise is resolved, the wizard navigates to the step. If the promise is rejected, the wizard stays on the current step and displays an error message.
This allows you to perform asynchronous operations and ensure that they complete before the user navigates to the step.
1 const { step, nextStep, previousStep, handlerPromise } = useWizard(); 2 3 const handleStep = async (step) => { 4 if (step === 2) { 5 handlerPromise.current = fetch('/api/data'); 6 } 7 }; 8 9 useEffect(() => { 10 if (handlerPromise.current) { 11 handlerPromise.current.then(() => { 12 nextStep(); 13 }).catch((error) => { 14 console.error(error); 15 }); 16 } 17 }, [step, handlerPromise, nextStep]); 18 19 return ( 20 <div> 21 {step === 1 && <Step1 nextStep={nextStep} />} 22 {step === 2 && <Step2 nextStep={nextStep} handleStep={handleStep} />} 23 {step === 3 && <Step3 previousStep={previousStep} />} 24 </div> 25 ); 26
The async function handleStep is a function that is called when the user navigates to a step. This function can be either sync or async, and it can return a promise. If the function returns a promise, the wizard will wait for the promise to resolve before navigating to the step.
This allows you to perform asynchronous operations, such as fetching data from an API or saving data to a database, and ensure that they are complete before the user navigates to the next step.
1 const handleStep = async (step) => { 2 if (step === 2) { 3 await fetch('/api/data'); 4 } 5 }; 6 7 const { step, nextStep, previousStep } = useWizard(); 8 9 return ( 10 <div> 11 {step === 1 && <Step1 nextStep={nextStep} />} 12 {step === 2 && <Step2 nextStep={nextStep} handleStep={handleStep} />} 13 {step === 3 && <Step3 previousStep={previousStep} />} 14 </div> 15 ); 16
In conclusion, the React Wizard is a powerful tool that provides a common UI design pattern for creating multi-step forms or processes. It simplifies the process of creating complex forms and processes, making it easier to collect and manage user input. With its flexible and customizable design, the React Wizard can be used to create a wide range of multi-step processes, from simple forms to complex workflows.
The React Wizard is built using the React use wizard, a custom hook that provides all the necessary functionalities to create a wizard in your React application. This includes handling the current step, navigating between steps, and managing the state of each step. With its support for asynchronous operations and promise-based navigation, the React Wizard is a flexible and powerful tool for creating interactive and dynamic UIs.
Whether you're building a simple form or a complex workflow, the React Wizard can help you create a user-friendly and engaging UI that guides the user through each step of the process. So why not give it a try and see how it can enhance your next React project?
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.