简体   繁体   中英

how can fix refresh problem in react router

i have a strange problem in my react application, i have two phase in my app, first is normal users that is web page and the other is dashboard for admins. in my dashboard when i refresh the page where ever i been the page redirects to /dashboard, for example i'm in /dashboard/cartable/requests then i refresh the page and page go to /dashboard page. my codes is in below: my app.js:

...
<Route exact path='/dashboard/' component={Index} />
            <Route path='/dashboard/login' component={Index} />
            <Route path='/dashboard/cartable' component={Index} />
            <Route path='/dashboard/cartable/requests' component={Index} />
            <Route path='/dashboard/cartable/approved' component={Index} />
            <Route path='/dashboard/cartable/rejected' component={Index} />
            <Route path='/dashboard/elections-management' component={Index} />
            <Route path='/dashboard/statistics' component={Index} />
            <Route path='/dashboard/statistics/assignee' component={Index} />
            <Route path='/dashboard/statistics/participation' component={Index} />
            <Route path='/dashboard/statistics/candidate-report' component={Index} />
            <Route path='/dashboard/change-pass' component={Index} />
            <Route path='/dashboard/errors' component={Index} />
...

in my index.js:

...
<Route exact path='/dashboard/' component={Layout} />
            <Route exact path='/dashboard/cartable' component={Layout} />
            <Route path='/dashboard/cartable/requests' component={Layout} />
            <Route path='/dashboard/cartable/approved' component={Layout} />
            <Route path='/dashboard/cartable/rejected' component={Layout} />
            <Route path='/dashboard/elections-management' component={Layout} />
            <Route exact path='/dashboard/statistics' component={Layout} />
            <Route path='/dashboard/statistics/assignee' component={Layout} />
            <Route path='/dashboard/statistics/participation' component={Layout} />
            <Route path='/dashboard/statistics/candidate-report' component={Layout} />
            <Route path='/dashboard/change-pass' component={Layout} />
            <Route path='/dashboard/errors' component={Layout} />
...

in my layout.js:

...
<Route exact path='/dashboard' component={Requests} />
                                <Route exact path='/dashboard/cartable' component={Requests} />
                                <Route path='/dashboard/cartable/requests' component={Requests} />
                                <Route path='/dashboard/cartable/approved' component={Approved} />
                                <Route path='/dashboard/cartable/rejected' component={Rejected} />
                                <Route path='/dashboard/elections-management' component={ElectionManagement} />
                                <Route exact path='/dashboard/statistics' component={Participation} />
                                <Route path='/dashboard/statistics/assignee' component={Assignee} />
                                <Route path='/dashboard/statistics/participation' component={Participation} />
                                <Route path='/dashboard/statistics/candidate-report' component={CandidateReport} />
                                <Route path='/dashboard/change-pass' component={ChangePassword} />
                                <Route path='/dashboard/errors' component={Errors} />
...

I believe that could get fixed if you move the "/dashboard" route to the end of the list

Wrap your Route by Switch like this

<Switch>
   <Route
   ...

</Switch>

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