javascript - How to return jsx after setTimeout - Stack Overflow

I have a loading state that I want to show only if the loading takes more than 1 second (since I don�

I have a loading state that I want to show only if the loading takes more than 1 second (since I don't want to keep showing the loader even if the loading time is just a few milliseconds).

This is the one that shows the loader regardless of the speed of loading:

if(loading) {
    return <div className="loader" />;
}

This is the one with setTimeout:

if(loading) {
    setTimeout(function() {
        return <div className="loader" />;
    }, 1000);
}

I understand that the return statement applies to setTimeout and not the ponent that's why it doesn't work.

Should I then create a state first (e.g. isLoadingSlow) which I will change to true after 1 second in the setTimeout callback? Then based from that state, then I would do a return statement with the loader jsx?

I have a loading state that I want to show only if the loading takes more than 1 second (since I don't want to keep showing the loader even if the loading time is just a few milliseconds).

This is the one that shows the loader regardless of the speed of loading:

if(loading) {
    return <div className="loader" />;
}

This is the one with setTimeout:

if(loading) {
    setTimeout(function() {
        return <div className="loader" />;
    }, 1000);
}

I understand that the return statement applies to setTimeout and not the ponent that's why it doesn't work.

Should I then create a state first (e.g. isLoadingSlow) which I will change to true after 1 second in the setTimeout callback? Then based from that state, then I would do a return statement with the loader jsx?

Share Improve this question asked Oct 11, 2018 at 7:00 catandmousecatandmouse 11.8k24 gold badges95 silver badges158 bronze badges 1
  • I don't think there is a way yet to detect how much time the ponent is taking to load. React has proposed this feature of async rendering in the future version though – Shubham Khatri Commented Oct 11, 2018 at 7:11
Add a ment  | 

1 Answer 1

Reset to default 4

NOTE: this is an answer written based on an answer that was deleted from this post.

I think you need to have two variables, one is loader, another one is show loader. After one second, if it is still loading, we should update the stat to show the loader, otherwise it won't be shown.

state= {
   loading: true,
   showLoader: false
}

ponentDidMount() {
    setTimeout(() => {
      if(this.loading){
          this.setState({ showLoader: true })
      }
    }, 1000);
}
render() {
   if(this.state.showLoader) {
        return <div className="loader" />;
   }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744970856a4603957.html

相关推荐

  • javascript - How to return jsx after setTimeout - Stack Overflow

    I have a loading state that I want to show only if the loading takes more than 1 second (since I don�

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信