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

161
Views
React acces DOM elements from JS file

So i'd like to handle element clicks but in a external js file. (my js file is in the public folder)

 import React, { Component } from 'react';
 import './App.css'; 
import './App.css';

function App() {
    return (
<div className="App">
   <button id="testbtn">asd</button>
</div>
    );
}
export default App;

This is my App.js code (this file is in the src/ folder) In the public folder there is my main.js file which has been imported in index.html (import was successful console.log works fine)

document.getElementById("testbtn").onclick = function () {
  console.log("asd");
};

I aslo got an error:

Uncaught TypeError: Cannot set properties of null (setting 'onclick')

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!