0

我正在尝试在下一个 js 中使用带有 react-visibility-sensor 的 react-odometer js。在这里我得到一个像图像一样的错误?我怎样才能摆脱这个错误,请专家帮忙。 在此处输入图像描述

这是我的代码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>
  );
}
4

1 回答 1

0

将里程表包裹在一个div中,就像这样

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

您应该会看到“500”渲染。

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

于 2021-09-10T07:40:43.187 回答