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

192
Views
reactoring insertBefore in reactjs

I have a button that when clicked wants to insert an element before it. here is the code;

<div><button onClick={(e) => {
    let container = (e.target as HTMLElement).parentNode?.parentNode;
    if (container) {
        let div = document.createElement("div")
        div.classList.add(styles.card)
        container.insertBefore(div, container.lastChild)
        ReactDOM.render(<MyReactElement/>, div)
    }
}}>add</button></div>

This feels terribly sloppy and I want to refactor it so that it uses either all react-dom or all dom, I think this will be a more efficient method. The method above is the only way i've been able to figure out how to achieve the result I am looking for.

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