[英]How to make react router replace component only after the matched route asynchronously loaded..?
So here's the situation - When a Link
is clicked the nprogress bar will start and I want react-router to only replace the current component with the matched route once that's done loading asynchronously.. just like in instagram..所以这里的情况 - 当一个
Link
被点击时,nprogress 栏将启动,我希望 react-router 在异步加载完成后只用匹配的路由替换当前组件..就像在 instagram 中一样..
But I am only getting this -但我只得到这个 -
Here's my HOC to load component asynchronously --这是我异步加载组件的 HOC——
import React, { useEffect, useState } from "react"; import nprogress from "nprogress"; import "nprogress/nprogress.css"; export default importComponent => props => { const [loadedComponent, setComponent] = useState(null); // this works like componentwillMount if (!nprogress.isStarted()) nprogress.start(); if (loadedComponent) nprogress.done(); useEffect(() => { let mounted = true; mounted && importComponent().then( ({ default: C }) => mounted && setComponent(<C {...props} />) ); // componentUnMount return () => (mounted = false); }, []); // return the loaded component const Component = loadedComponent || <div style={{ flexGrow: 1 }}>..</div>; return Component; };
I didn't find a solution to this anywhere on the internet.. so I am asking this question here in stackoverflow.我没有在互联网上的任何地方找到解决方案..所以我在stackoverflow中问这个问题。 I am hoping someone here can solve this.
我希望这里有人可以解决这个问题。
I was having the same problem today.我今天遇到了同样的问题。 I had to keep track of the previous page component and return it while loading the next page.
我必须跟踪上一页组件并在加载下一页时返回它。 As below,
如下,
if(!loadedComponent) return prevPage;
prevPage = loadedComponent;
return loadedComponent;
I just declared prevPage outside the component because using useState caused infinite rendering.我只是在组件外声明了 prevPage 因为使用 useState 会导致无限渲染。 But I think there might be a reactive approach for storing prevPage.
但我认为可能有一种存储 prevPage 的反应式方法。 Here is an example gist .
这是一个示例gist 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.