Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Return different <Input> in a form using a switch to return the Input

I'm trying to achieve a Form where first you see 2 and then when you click a button, you see other Input. I'm using a switch where based on a value I return the first 2 or the others. I've tried returning only the , returning a form with the inputs but everything gives me the same problem. After i fill the first two text input, when i switch to the others, the first 2 of those input has the values of the previous 2 input.

Thanks for your help.

Here there is a basic version of the code

function renderSwitch() {
  switch (currentPhase) {
    case 0:
      return (
        <div>
          <input name='email' type='email' placeholder='Inserisci email' />
        </div>
      );
      break;

    case 1:
      return (
        <div>
          <input
            name='surname'
            type='text'
            placeholder='Inserisci Cognome...'
          />
        </div>
      );
      break;
  }

  return (
    <div
      className='registerpage'
      style={{ height: '100vh', backgroundColor: 'white' }}
    >
      <div className='formdiv'>
        <form onSubmit={handleSubmit}>
          {renderSwitch()}

          {currentPhase < 1 && (
            <button onClick={() => nextPhase()}>Next</button>
          )}
          {currentPhase > 0 && (
            <button onClick={() => previousPhase()}>Back</button>
          )}
          <br />
          <button type='submit'>Iscriviti</button>
        </form>
      </div>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should store the input values using a state:

const [email, setEmail] = useState('')
const [surname, setSurname] = useState('')

function renderSwitch() {
    switch (currentPhase) {
      case 0:
        return (
          <div>
            <input value={email} name='email' type='email' placeholder='Inserisci email' />
          </div>
        );
        break;
  
      case 1:
        return (
          <div>
            <input
            value={surname}
              name='surname'
              type='text'
              placeholder='Inserisci Cognome...'
            />
          </div>
        );
        break;
    }
}

And then reset them once you have submitted your data:

function handleSubmit(e) {
    e.preventDefault()
    // Handler your data...

    setEmail('')
    setSurname('')
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!