Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Create a Multi-Step Form with ReactJS

Megan Lo
Dev Genius
Published in
10 min readMay 13, 2021

--

Photo by inlytics | LinkedIn Analytics Tool on Unsplash

Why Multi-Step Form?

Walkthrough

🔍 Code Setup (Optional if you have an existing React app set up) 🔍

npx create-react-app multi-step-form-tutorial
cd multi-step-form-tutorial
code .
yarn start 
App.test.jslogo.svgsetupTests.js
Current App.js

🥁 Step 1: Organize how many steps you want and what information you want to include in each step 🥁

App.js
└───Signup.js
├───UserDetails
├───PersonalDetails
├───Confirmation
└───Success

🥁 Step 2: Set up our components🥁

Signup.js
UserDetails.js
App.js

🥁 Step 3: Define state in Signup.js 🥁

state — Signup.js

🥁 Step 4: Define methods in Signup.js 🥁

// go back to previous step
prevStep = () => {
const { step } = this.state;
this.setState({ step: step - 1 });
}
// proceed to the next step
nextStep = () => {
const { step } = this.state;
this.setState({ step: step + 1 });
}
// handle field change
handleChange = input => e => {
this.setState({ [input]: e.target.value });
}
methods — Signup.js

🥁 Step 5: Import and pass states to the forms component into Signup.js 🥁

const { step } = this.state;const { email, username, password, firstName, lastName, country, levelOfEducation } = this.state;const values = { email, username, password, firstName, lastName, country }
switch (step) {
case 1:
return (
<UserDetails />
)
case 2:
return (
<PersonalDetails />
)
case 3:
return (
<Confirmation />
)
case 4:
return (
<Success />
// never forget the default case, otherwise VS code would be mad!
default:
// do nothing
}
switch case — Signup.js
Second half of Signup.js

🥁 Step 6: Add Information to UserDetails/PersonalDetails🥁

destructure — UserDetails.js
// with npm
npm install @material-ui/core
// with yarn
yarn add @material-ui/core
<label>Email
<input
type="text"
placeholder="email address"
value={values.email}
onChange={handleChange('email')}
/>
</label>
<TextField
placeholder="Email Address"
label="Email Address"
onChange={handleChange('email')}
defaultValue={values.email}
/>

🥁 Step 7a: Create “Next” button to continue 🥁

const Continue = e => {
e.preventDefault();
nextStep();
}
<button onClick={ Continue }>Next</button>

🥁 Step 7b: Create “Previous” button to go back to the previous step🥁

const Previous = e => {
e.preventDefault();
prevStep();
}
<button onClick={ Previous }>Previous</button>
PersonalDetails.js

Demo

I apologize for the white-to-white background 😅

Before You Go…

More Resources:

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Megan Lo

Software Engineer @ Citi | I write about JavaScript | In Writing Hiatus.