Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

106
Vistas
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 Respuestas
Responde la pregunta

0

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

        useEffect(() => {
           setTimeout(()=> {
               fetchChartData();
            }, 2000)
         }, []);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda