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

93
Views
function does not wait for DOM to load

when I run this code I get an error saying I cannot set innerText of Null; I know this is because my function runs before my DOM fully loads in, but I do not know how to fix it.

I have tried using window.onload, and event listeners for loading, but to no avail.

Any help is appreciated, thanks !

import "./styles.css";
const quoteBox = document.getElementById("App");

async function newQuote() {
  const response = await 
  fetch("https://api.quotable.io/random");
  const data = await response.json();
  quoteBox.innerText = data.content;
}

window.onload = newQuote();

export default function App() {
 return <div className="App" id="App"></div>;
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use useEffect so that function gets called after it is mounted.

export default function App() {

 useEffect(() => {
    
   const quoteBox = document.getElementById("App");

   async function newQuote() {
     const response = await 
     fetch("https://api.quotable.io/random");
     const data = await response.json();
     quoteBox.innerText = data.content;
  }

  newQuote();

 }, []);

 return <div className="App" id="App"></div>;
}

Refer: https://reactjs.org/docs/hooks-effect.html

about 4 years ago · Juan Pablo Isaza Report

0

Either add your script tag at the end of your body tag or wrap everything in a function and call it on DOMContentLoaded

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!