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

135
Views
what React version of appending to the DOM onClick event>

I want to add "<li>" element to the box element onclick the "innerButton", without needing to ID any element so I can add as many boxes as I need but how?


export default class App extends Component {
    
    render()
    {

      return <>
      Home
      
      <BoxA/>
      <BoxB/>
      <InnerButton name='Enter'/>

    </>
    }
  }



  function InnerButton(arr)
  {
    function addItemToList(newItem)
    {
        // what to do
    }
  
    console.log(arr)
    return <input type={'button'} value={arr.name} }
    onClick={() => addItemToList('item 2')}
    />
  }
function BoxA()
{  

  
  
  return <div id='boxA' >
    <ul>
      <li>
        None.
      </li>
    </ul>
  </div>

}

function BoxB()
{  
  return <div >
    <ul>
      <li>
        None.
      </li>
    </ul>
  </div>
}

I tried adding useState ,but I can't use it outside the component ,so can I do it without using states?

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

0

store an array of items you want to list in the state at the top level (App), and pass a push function to to InnerButton as a prop, and pass the array to the Box elements.

In the Box element map through the array props and render an <li></li> for each element

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!