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

103
Views
useEffect not printing div element react hooks

I have this Chart component. useEffect works as componentDidMount() so it should first render the DOM and then should call the fetchChartData. But the issue I am facing here is I get null in console.log("111111111", document.getElementById("data-consumed")) at first and after that I get the DOM elements and resulting I get error from the Highcharts

Uncaught (in promise) Error: Highcharts error #13

So I need to know why this is happening as useEffect works as componentDidMount and should render the DOM first.

const Chart2 = (props) => {
  const fetchChartData = async () => {
    const { fetchChartData } = props;
    const startDate = moment().startOf("year").toISOString();
    const endDate = moment().endOf("year").toISOString();
    const data = await fetchChartData({ startDate, endDate, userId: "XXXXXXX" });
    console.log("111111111", document.getElementById("data-consumed"))
    Highcharts.chart("data-consumed", {
      chart: {
        type: "column",
      },
      title: {
        text: null,
      },
      subtitle: {
        text: "Source: WorldClimate.com",
      },
      xAxis: {
        categories: data.map(({ _id }) => _id),
        crosshair: true,
      },
      yAxis: {
        min: 0,
        title: {
          text: "Rainfall (mm)",
        },
      },
      plotOptions: {
        column: {
          pointPadding: -0.1,
          borderWidth: 0,
          color: "#5036D6",
        },
      },
      series: [
        {
          name: null,
          data: data.map(({ processingStorage }) => processingStorage),
        },
      ],
    });
  };

  useEffect(() => {
    fetchChartData();
  }, []);

  return (
    <div className="chart-container cursor-pointer">
      <div className="chart-block">
        <h4>Data Consumed</h4>
        <div id="data-consumed" className="graph-block" />
      </div>
    </div>
  );
};

export default Chart2;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try using a setTimeout function. Call the fetchChartData function inside setTimeout

        useEffect(() => {
           setTimeout(()=> {
               fetchChartData();
            }, 2000)
         }, []);
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!