Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
How to add a new div on button click with React

I'm trying to add a new input on button click in one of my React components. What's the most elegant way to do it?

This is an abstraction of my code:

const addDivs = (type) => { ??? }

export default function Form() {

handleSubmit(e) {...}

return ( 
<form onSubmit={handleSubmit}>
<div><textarea rows="4" placeholder="Write about something here..." /></div>
//New divs should go here

//Adds a textarea
<button onClick={() => addDivs(textarea)}>Add textarea</button>
//Adds an upload field
<button onClick={() => addDivs(upload)}>Add image</button>
<input type="submit" value="Submit" /> </form> ); 
}

I want to keep the buttons underneath the inputs.

How's the best way to do this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

My idea is to create 2 states, one for each button, they will have the number of the divs needed for each tag.

const [textAreaNo,setTextAreaNo] = useState(0);
const [uploadNo,setUploadNo] = useState(0);

then after your buttons you can call the (addDiv) function, but u will have to create 2 different functions for each button, or just make one function and inside the function it will return upload or textArea depending on the passed parameter.

then finally you need to use map like this

{[...Array(textAreaNo)].map(e=>(
    <div> textbox <div>
  ))}

and on your button we will just increment the states above like

 <button onClick={()=> setTextAreaNo(textAreaNo+1)}>AddDivs</button>
about 4 years ago · Juan Pablo Isaza Relatório

0

I'm not sure if you want to be able to add multiple text areas. A way I would do it, is to create a state item that knows how many text areas you need, and then a function that increases this number on the onClick(). You will also need a function that returns the text box item. Something like this:

Class Component:

this.state = {
    numberOfTextAreas: 0
}

const textAreas = () => { //<---------a function like this

for (let i = 0; i < this.state.numberOfTextAreas; i++) {
    return (
        <div>
           <textarea rows="4" placeholder="Write about sompething here..." /> 
        </div>
    );
}
}

const addDivs = (type) => { ??? }

export default function Form() {

handleSubmit(e) {...}

return ( 
<form onSubmit={handleSubmit}>
<div><textarea rows="4" placeholder="Write about something here..." /></div>


//New divs should go here
{textAreas} //<---- Add this here.


<button onClick={() => this.setState({numberOfTextAreas + 1})}>Add textarea</button>
//Adds an upload field
<button onClick={() => addDivs(upload)}>Add image</button>
<input type="submit" value="Submit" /> </form> ); 
}

```

Then you'll just want to do the same with the other types you are wanting to create.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda