Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
Using React, how do I create a textfield only when a button is clicked and keep on creating more textfields when the button is clicked?
const [showNotes, setShowNotes] = useState(false);

const generateNotes = () => {
    setShowNotes(true);
    <TextField id="notesField" label="Add your note here" variant="outlined"/>
};

<div style = {{ display: 'flex', width: '300px', justifyContent: 'space-between' }}>
        
    <Button id="note" onClick={generateNotes} variant="contained" component="label" style = {{ backgroundColor: '#3f78b5', flex: '50px', width: '122px', height: '38px', 
          borderRadius: '8px', left: '388px', position: 'absolute' }}>
          Add Note
    </Button>
</div>

I created a useState variable and initialized it to false. Then, I created a function that would set it to true and create the textfield. Then when the button is clicked it should display the textfield but that is not happening. I realize I did not set the showNotes variable anywhere but I am not sure on what to set to that.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you can do this by having an array that the button appends some text to, and react will create a TextField component for every item, example:

const [notes, setNotes] = useState([]);

const generateNotes = () => {
    notes.push('some label');
    setNotes([ ...notes ]);
};

return (
    <div>
        
        <Button id="note" onClick={generateNotes}></Button>

        {
            notes.map((item, index)=>{
                return <TextField key={index} label={item)/>
            }
        }

    </div>
)
about 4 years ago · Juan Pablo Isaza Denunciar

0

You cannnot render TextField inside a callback, it should be from your component return.You can create a counter to increment the number of input fields and render only if count is greater than zero.

const [inputFieldCount, setInputFieldCount] = useState(0);

const generateNotes = () => {
  setInputFieldCount((count) => count + 1);
};

return (
  <div
    style={{ display: "flex", width: "300px", justifyContent: "space-between" }}
  >
    <Button onClick={generateNotes}>Add Note</Button>
    {inputFieldCount > 0 && Array.from({ length: inputFieldCount }, (_, index) => (
      <TextField
        key={index}
        id="notesField"
        label="Add your note here"
        variant="outlined"
      />
    ))}

  </div>
);

The second argument from Array.from is a map function to iterate over the array count.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda