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

170
Visualizações
How to prevent re-rendering upon adding new React component to form

TLDR: When I click a button to add a new element to a list of React components, it causes the entire list to rerender and lose the states in the process. How can I prevent this?


I have a list of components containing a textarea and a few other form elements.

Excerpt:

        <FormControl  as='textarea'  onChange={e => setTopicSents(e.target.value)}></FormControl>

As you can see, I have the form input stored in a state within the component itself. I have the "Topic" component defined within a "Topics" component which renders all of the text areas.

I have a button that, upon clicking, adds a new FormControl element to the "topics" array:

        const [topics, setTopics] = useState([0])
        function addNewTopic(){
            setTopics([...topics, topics+1])
        }

            <div>
                {topics.map((i)=>  <Topic></Topic> )}
                <Row style={{height: "3em"}}>
                <div>
                    <Button onClick={addNewTopic} className="cyan-btn">Add New 
                   Topic</Button>
                </div>
            </Row>
            </div>

The problem is that when I click the button to add a new form element, it causes the entire list to rerender and clears anything that the user has typed in the form. How can I change this so that either the states are preserved upon adding a new form element, or the addition of a new one does not cause the rerendering of the other form elements?

I hope that makes sense. Any help would be appreciated!

about 4 years ago · Juan Pablo Isaza
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