简体   繁体   English

React Hooks:加载 state 显示两次

[英]React Hooks: Loading state displaying twice

I have an isLoading useState variable.我有一个isLoading useState 变量。 In the code below purposefully commented out the isLoading(false) to force the Loading UI to test it.在下面的代码中,特意注释掉了isLoading(false)以强制加载 UI 对其进行测试。 However I'm seeing it render twice, any idea why?但是我看到它渲染了两次,知道为什么吗?

在此处输入图像描述

API Call API 调用

export default function App() { 
  const [upcoming, setUpcoming] = useState([]); 
  const [isLoading, setIsLoading] = useState(false);

 useEffect(() => {
    const fetchUpcoming = async () => {
      setIsLoading(true);

      const res = await fetch(
        "https://api.themoviedb.org/3/movie/upcoming?api_key={API_KEY}&language=en-US&page=1"
      );
      const data = await res.json();
      const results = data.results;

      setUpcoming(results);
      // setIsLoading(false);
    };

    fetchUpcoming();
  }, []);

return (
    <div className="App">
      <Recommendations title={"Upcoming"} data={upcoming} loading={isLoading} />
    </div>
  );
}

Render Results渲染结果

export default function Recommendations({ title, data, loading }) {
  return (
    <div className="recommendationSection">
      <h3>{title}</h3>
      {loading ? (
        <h3>Loading...</h3>
      ) : (
        data.map((movie) => {
          return (
            <div className="banner" key={movie.title}>
              <img
                src={
                  movie.poster_path
                    ? `https://image.tmdb.org/t/p/original/${movie.poster_path}`
                    : "https://www.genius100visions.com/wp-content/uploads/2017/09/placeholder-vertical.jpg"
                }
                alt={movie.title}
              />
            </div>
          );
        })
      )}
    </div>
  );
}

Found the answer here: https://github.com/kenwheeler/slick/issues/940#issuecomment-181815974在这里找到答案: https://github.com/kenwheeler/slick/issues/940#issuecomment-181815974

I found the second instance had a class of slick-cloned and basically since i'm using Slick Slider, it's making a clone of my element.我发现第二个实例有一个slick-cloned的 class,基本上因为我使用的是光滑 Slider,它正在克隆我的元素。 The fix is to add infinite: false in my slider settings.解决方法是在我的 slider 设置中添加infinite: false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM