[英]React multiple routes using the same component but with different parameters
I have one Login
component with multiple states, depending on the url I'd like to display different elements within this component.我有一个具有多个状态的Login
组件,具体取决于 url 我想在此组件中显示不同的元素。 Here is how I've set up my Routes
这是我设置Routes
的方式
import {
BrowserRouter,
Routes,
Route,
} from "react-router-dom";
// Pages
import Home from '../pages/home';
import Login from '../pages/login';
export default function App() {
// Render
return (
<div className="app">
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />}/>
<Route exact path="/login" element={<Login state="login"/>}/>
<Route exact path="/signup" element={<Login state="signup"/>}/>
<Route exact path="/reset-password" element={<Login state="reset"/>}/>
</Routes>
</BrowserRouter>
</div>
);
}
I'm using BrowserRouter v6 and this code is working fine whenever I load / reload the page.我正在使用 BrowserRouter v6,每当我加载/重新加载页面时,这段代码都可以正常工作。 However using a <Link to="/reset-password"></Link>
to go to from one Login
page to another does not appear to remount the components on the page.但是,使用<Link to="/reset-password"></Link>
到 go 从一个Login
页面到另一个页面似乎不会重新安装页面上的组件。
I have seen some exemples using <Route exact path="/login" component={...}/>
with BrowserRouter v5 but I can't seem to get it to display anything when using component
instead of element
with this version.我已经看到一些将<Route exact path="/login" component={...}/>
与 BrowserRouter v5 一起使用的示例,但在使用此版本的component
而不是element
时,我似乎无法让它显示任何内容。
I there something I'm missing or am I going about this all wrong?我有什么我想念的东西还是我做错了?
One new feature from v6 is the new Outlet component. v6 的一项新功能是新的 Outlet 组件。 See if creating a layout with an outlet can help you: https://reactrouter.com/docs/en/v6/examples/basic查看创建带有插座的布局是否可以帮助您: https://reactrouter.com/docs/en/v6/examples/basic
i was having this type of problem.我有这种类型的问题。 i had a table as a child component into a parent components of two different routes with different filters.我有一个表作为子组件到具有不同过滤器的两条不同路由的父组件中。 Async call for data was into parent component and it was being set to redux store.对数据的异步调用已进入父组件,并且被设置为 redux 存储。 table was getting data from that store.表正在从该商店获取数据。
problem was that,when i switch between these two routes,it was getting a delay to update data in table.问题是,当我在这两条路线之间切换时,更新表中的数据会出现延迟。
so i fixed problem by clearing redux state on unmount of child componant (table).所以我通过在卸载子组件(表)时清除 redux state 来解决问题。 like this像这样
useEffect(() => {
return ()=>{
// cancel previous API calls that are being used to list data
//clear data from redux store
}
},[dispatch])
here the point to be noted that this code should be written in child component这里需要注意的一点是这段代码要写在子组件中
(in my case Table). (在我的例子中是表)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.