简体   繁体   English

仅在异步加载匹配的路由后,如何使反应路由器替换组件..?

[英]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.

相关问题 如何检测来自外部组件的匹配路由<route />使用 react-router 匹配的组件? - How to detect matched route from a component outside of the <Route/> component that was matched using react-router? React组件未显示在匹配的路由上(react-router-dom) - React Component not showing on matched Route (react-router-dom) 从 cookies 异步加载身份验证令牌时,反应 react-router-dom 私有路由不起作用 - React react-router-dom private route not working when auth token is asynchronously loaded from cookies 如何用react-router替换react组件 - How to replace a react component with react-router react-router v4:判断路由是否从组件外部匹配 - react-router v4: Determine if route has been matched from outside the component React router 4`Link`组件只更改url而不更新路由 - React router 4 `Link` component only changing the url and not updating the route React router Link 只渲染父组件中指定的路由 - React router Link only renders the route specified in parent component 在我的父组件中使用 React Router 登录后如何重新路由? - How can I re-route after a sign-in using React Router in my parent component? 路由匹配时,React Router不显示组件(但渲染有效) - React Router doesn't display component when route is matched (but render works) 响应`Router`组件如何与`Route`组件通信? - How does the react `Router` component communicate with the `Route` component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM