javascriptreactjsnext.jsfrontendodometer

el.getBoundingClientRect is not a function in react odometer js with react visibility sensor?


I am trying to use react-odometer js with react-visibility-sensor in next js. Here I am getting one err like the image? How can I get rid of this error, Experts please help. enter image description here

Here is my code https://codesandbox.io/s/summer-dream-ysi00

import { useState, useEffect } from "react";
import dynamic from "next/dynamic";
import "odometer/themes/odometer-theme-default.css";
const Odometer = dynamic(import("react-odometerjs"), {
  ssr: false,
  loading: () => 0
});
import VisibilitySensor from "react-visibility-sensor";

export default function IndexPage() {
  const [odometerValue, setOdometerValue] = useState(0);
  const [view, setView] = useState(false);
  const onVisibilityChange = (isVisible) => {
    if (isVisible) {
      setView(true);
    }
  };
  useEffect(() => {
    setTimeout(() => {
      setOdometerValue(500);
    }, 10);
  }, []);
  return (
    <VisibilitySensor onChange={onVisibilityChange} offset={8} delayedCall>
      <Odometer
        value={view ? odometerValue : 0}
        format="(,ddd)"
        theme="default"
      />
    </VisibilitySensor>
  );
}

Solution

  • wrap Odometer in a div, like so

    <VisibilitySensor onChange={onVisibilityChange} offset={8} delayedCall>
      <div>
        <Odometer
          value={view ? odometerValue : 0}
          format="(,ddd)"
          theme="default"
        />
      </div>
    </VisibilitySensor>;
    

    you should see '500' rendered.

    https://codesandbox.io/s/wonderful-fast-y3k4s?file=/pages/index.js