简体   繁体   English

使用相同的组件但使用不同的参数来响应多个路由

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

相关问题 使用 React Router 为不同的路由使用相同的组件 - Using the same component for different Routes with React Router 如何为具有不同价值道具的不同路线呈现相同的反应成分? - How to render same react component for different routes with different value props? 使用相同中继根查询和不同片段字段的多个react-router-relay路由 - Multiple react-router-relay routes using same Relay root query with different fragment fields 使用不同的路由但使用相同的组件 VueJS 3 获取数据 - Fetching data using different routes but using same component VueJS 3 将多条路线指向同一组件 - Pointing multiple routes to same component 使用 Vue JS 将来自不同路由的 props 发送到同一个组件 - Sending props from different routes to the same component using Vue JS 将同一个 Vue 组件的变化反映到不同的路由上 - Reflect changes of same Vue Component to different routes React Router 4为所有路由呈现相同的组件 - React router 4 render same component for all routes (Vue.js)具有不同路线的相同组件 - (Vue.js) Same component with different routes React 如何在同一个父组件上显示多个不同来源的组件 - React how to display multiple components of different origin on the same parent component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM