简体   繁体   中英

change url and re-render react router

I try to change the url with react router as below:

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>
           }
}

and in my components I have the code as below:

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);

But when I change the component with History.push or <Link/> the url is changed but re-render does not occur and NotFound Page will be shown and I should refresh the page and then the component will be shown. what should I do?

我的错误是我{Router} from 'react-router-dom'导入{BrowserRouter as Router} from 'react-router-dom'我应该{Router} from 'react-router-dom'导入{BrowserRouter as Router} from 'react-router-dom' ,我认为这是常见的错误,希望对您有所帮助。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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