繁体   English   中英

反应路由器更改 url 但不重新渲染

[英]React router changes url but not re-render

React Router 更改了 URL 但组件未呈现我已经在寻找答案但这些示例都没有工作当前 React Router 和 React Router DOM 版本为 6。我的 MainComponent:

 import React, { Component } from 'react'; import Header from './HeaderComponent'; import Footer from './FooterComponent'; import { Routes, Route, Redirect } from 'react-router-dom'; import Stafflist from './StaffComponent'; import {STAFFS} from '../shared/staffs'; import StaffDetail from './StaffDetailComponent'; class Main extends Component{ constructor(props){ super(props); this.state ={ staffs: STAFFS }; } render(){ const StaffWithId = ({match}) =>{ return( <StaffDetail staff={this.state.staffs.filter((staff) => staff.id === parseInt(match.params.staffId,10))}/> ) } return( <div> <Header/> <Routes> <Route exact path='/staff' element={<Stafflist staffs={this.state.staffs} />}/> <Route path='/staff/:staffId' element={StaffWithId}/> </Routes> <Footer/> </div> ); } } export default Main;

您的问题似乎是这一行:
<Route path='/staff/:staffId' element={StaffWithId}/>

StaffWithId 是一个功能组件,应该用括号< />调用。
像这样: <Route path='/staff/:staffId' element={<StaffWithId/>}/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM