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

245
Views
how to change text to input field onclick in react?

I want to change the Text to input field when clicking on edit button. The Text is inside map function, The Code that I have worked is mentioned below

function test() {
  const [img, setImg] = useState(false)

  let data = {
    {  
      id: 1,
      name: 'test 1'
    },
    {
      id: 2,
      name: 'test 2'
    }
  }

return (
  <>
    {data.map(e => {
      return (
        {img ? (
          <input placeholder={e.name} />
        ) : (
          <div>
            <p>{e.name}</p>
            <button onClick={() => setImg(true)}>edit</button>
          </div>
        )}
      )
    })}
  </>
)

In this code if i click on edit button, it actually shows input in all name, rather than which i click. i want to edit only one name, for example, if i want to edit name test 1, i dont want the input to show on test 2

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Something like this would work -

function test() {
  const [img, setImg] = useState(-1)

  let data = {
    {  
      id: 1,
      name: 'test 1'
    },
    {
      id: 2,
      name: 'test 2'
    }
  }

return (
  <>
    {data.map((e, idx) => {
      return (
        {img==idx ? (
          <input placeholder={e.name} style={{display: idEdit == e.id ? 'block' : 'none'}}/> //<== Conditionaly to appear or not
        ) : (
          <div>
            <p>{e.name}</p>
            <button onClick={() => {
               setImg(idx)
              } 
             }>edit</button>
          </div>
        )}
      )
    })}
  </>
)
about 4 years ago · Juan Pablo Isaza Report

0

You can set a state when you click edit button, then use this state to conditionally show of input you want:

function test() {
  const [img, setImg] = useState(false)
  const [idEdit, setIdEdit] = useState(null); //<== Create this state

  let data = {
    {  
      id: 1,
      name: 'test 1'
    },
    {
      id: 2,
      name: 'test 2'
    }
  }

return (
  <>
    {data.map(e => {
      return (
        {img ? (
          <input placeholder={e.name} style={{display: idEdit == e.id ? 'block' : 'none'}}/> //<== Conditionaly to appear or not
        ) : (
          <div>
            <p>{e.name}</p>
            <button onClick={() => {
               setImg(true)
               setIdEdit(e.id) //<== set idEdit state here
              } 
             }>edit</button>
          </div>
        )}
      )
    })}
  </>
)
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!