简体   繁体   English

如何先显示加载程序,然后根据条件加载结束后显示文本?

[英]How to display loader initially and then display text after loading ends based upon condition?

I want to display a loader initially and after a certain time when the loader is turned off manually based upon the condition I want to either fun a function or display a heading 1.我想最初显示一个加载程序,并在加载程序根据我想要玩 function 或显示标题 1 的条件手动关闭加载程序的特定时间后显示。

I want that when initially when the page loads, the loader should be displayed but after a certain time it turns off maybe with the help of setTimeout, and if the length of the array (videoIds) is 0 I want to display the heading "Enter at least one video in the collection first" otherwise if the length is not zero then run fetchFunc() and display result and thus render component.我希望最初加载页面时,应该显示加载器,但在一定时间后它可能会在 setTimeout 的帮助下关闭,如果数组的长度(videoIds)为 0,我想显示标题“Enter首先在集合中至少有一个视频”,否则如果长度不为零,则运行 fetchFunc() 并显示结果,从而渲染组件。

I tried using promises but "Enter at least one video in the collection first" is already getting rendered when the page is loading.我尝试使用 promises,但在加载页面时已经呈现“首先在集合中输入至少一个视频”。 How can I achieve it?我怎样才能实现它?

 const [display, setdisplay] = useState();
  const [userMsg, setuserMsg] = useState(true);

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

  function displayFunc() {
    new Promise(function (myResolve) {
      myResolve(
        setTimeout(() => {
          setuserMsg(false);
        }, 2000)
      );
    }).then(() => {
      if (videoIds.length === 0) {
        setdisplay(false);
      } else {
        fetchFunc();
        setTimeout(() => {
          setdisplay(true);
        }, 2000);
      }
    });
  }

      {userMsg && <Loading />}
      {display ? (
        <ExploreCard videoArray={videoArray} explVidFunc={explVidFunc} />
      ) : (
        <h1>Enter at least one video in collection first</h1>
      )}
export const View = () => {
  const [videos, setVideos] = useState([]);

  const fetch = async () => {
    // fetch logic here
    setVideos(); // put response or data here
  });

  useEffect(() => {
    /*
      I don't believe the timeout is necessary, but if you want to
      delay the fetch function, you can do something like the following.
      setTimeout(() => fetch(), 3000); 
    */

    fetch();
  }, [videos]);

  return (
    <>
      {!!videos.length && <Loading />}
      {!videos.length ? (
        <ExploreCard
          {/* explVidFunc={explVidFunc */}
          videoArray={videoArray}
        />
      ) : (
        <h1>Enter at least one video in collection first</h1>
      )}
    </>
  );
}:
const [loading, setLoading] = useState(false)
const [videos, setVideos] = useState([])

const fetchVideos = async () => {
    try{
      // call api to fetch videos
      const res = callApi()
      setLoading(false)
      // change according to you response
      setVideos(res.data)
     
    } catch{
       setLoading(false)
}
}

useEffect(() => {
   fetchVideos()
},[])

Now, you can check for loading.现在,您可以检查加载。 If its true, show Loader else, check for videos.如果为真,则显示 Loader else,检查视频。 If its length is zero show some component else show different component.如果它的长度为零,则显示一些组件,否则显示不同的组件。

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

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