1

I want to make a multi-stage react form. In this particular code, only Zipcode will be asked and will be submitted on submitting the form. But how to bring other forms asking for Email, important thing~ I want the form for email after I submit Zipcode, as both email and zipcode will be sent to backend together.

import React, { useEffect, useState } from 'react';
const  Rider_Signup = ()=>{
 const [zipcode,setzipcode]=useState();
 const [email,set_email]=useState();
 const onSubmitform = async e =>{
     e.preventDefault();
     try{
         const body={zipcode,email};
         const response = await fetch("https://taxibackendf.herokuapp.com/api/service/signup",{
             method:"POST",headers:{"Content-Type":"application/json"},
             body:JSON.stringify(body)
         })
         const datainjson = await response.json();
         window.location =`/driver/login`;
     }catch(err){
         console.log('Error')
     }
 }
 return (   
  <div className="admin_form_div">
    

    <form  action="/initial" id="admin_form"  name="admin_form" onSubmit={onSubmitform}
            <input type="text" name="Zipcode" className="input" value={zipcode} 
             onChange={e =>setzipcode(e.target.value)} 
               />  
            <button type="submit" className="confirm_btn" >Confirm</button>
    </form> 
 </div>
 );
  };
   export default Rider_Signup;

1 Answer 1

2
const [step, setstep] = useState(1);

const [formdata, setFormData] = useState({zip:"", email:""}); // use to hold input from user 

const renderForm = () =>{
    switch(step){
        case 1: return <div>Form one with email<button onClick = {() => setstep(step+1)}>Submit</button></div>
        case 2: return <div>Form with zip code <button onClick = {() => setstep(step+1)}>Submit</button></div>
        default: return <div>Default case</div>


    }

}
return (

    renderForm()
)

`

Sign up to request clarification or add additional context in comments.

2 Comments

A newbie question for verification ~ <input type="email" name="name" className="Email" value={formdata.email} onChange={e =>setFormData(formdata.email=(e.target.value))}" required /> will this be correct format?
<input type="email" name="name" className="Email" value={formdata.email} onChange={e =>setFormData({...formdata, email:e.target.value})}" required />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.