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

163
Views
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 answers
Answer question

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 Report

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 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!