我尝试使用react router更改网址,如下所示:
const DashBoard = lazy(() => import("./dashboard/DashBoard"));
const ProjectSubmission = lazy(() => import('./projectsubmission/ProjectSubmission'));
const NotFound = lazy(() => import("./notfound/NotFound"));
class App extends Component {
render() {
<div className="App">
<header className="App-header">
<Router history={history}>
<Suspense fallback={}>
<Switch>
<Route path="/dashboard/" render={() => <DashBoard/>}/>
<Route path="/project-submission/" render={() => <ProjectSubmission/>}/>
<Route path="*" render={() => <NotFound/>}/>
</Switch>
</Suspense>
</header>
</div>
}
}
在我的组件中,我的代码如下:
import React from 'react';
import {connect} from "react-redux";
import {withRouter} from "react-router-dom";
class ProjectSubmission extends React.Component{
constructor(props){
super(props);
this.state = {location: ""}
}
render() {
return(
<div>
<h2>Project Submission</h2>
</div>
)
}
}
export default withRouter(ProjectSubmission);
但是,当我使用History.push
或<Link/>
更改组件时,URL会更改,但不会重新渲染,并且将显示NotFound
页面,我应该刷新页面,然后该组件将显示。我该怎么办?
答案 0 :(得分:0)
我的错误是我导入了{Router} from 'react-router-dom'
我应该导入{BrowserRouter as Router} from 'react-router-dom'
,我认为这是常见的错误,希望对您有所帮助。