最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Correctly handling the BFCache in react - Stack Overflow

programmeradmin2浏览0评论

I'm showing a loading screen (state) before redirecting to an external url (payment provider), since they tend to load for a while.

If the user chooses not to plete the payment and goes history.back() (Gesture, back button, ...) the browser pulls the page before from the BFCache, including the loading state, and the user will be "stuck" loading forever.

How would you suggest handling that? Persisted loads can be detected using the pageshow event, but I'm not sure if that is the react way to handle the case.

I'm showing a loading screen (state) before redirecting to an external url (payment provider), since they tend to load for a while.

If the user chooses not to plete the payment and goes history.back() (Gesture, back button, ...) the browser pulls the page before from the BFCache, including the loading state, and the user will be "stuck" loading forever.

How would you suggest handling that? Persisted loads can be detected using the pageshow event, but I'm not sure if that is the react way to handle the case.

Share Improve this question asked Feb 5, 2022 at 10:01 KimmaxKimmax 1,69722 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Persisted loads should be handled manually. In your case you should handle the
loader state based on the event property persisted from pageshow event handler.

I had the same issue, I ended up using pageshow event

const [isLoading, setIsLoading] = useState<boolean>(false);
useEffect(() => {
    window.onpageshow = function(event) {
      if (event.persisted) {
        setIsLoading(false);
      }
    };
  }, []);
发布评论

评论列表(0)

  1. 暂无评论