reactjs - Reactjs在点击重新加载按钮时重新加载当前路由,而不是整个应用程序
问题描述
我有一个基本的 react-router 设置,带有 Switch,如下所示:
import React from "react"
import {Route, Switch } from "react-router-dom"
import AuthenticatedRoute from "./components/AuthenticatedRoute"
import UnauthenticatedRoute from "./components/UnauthenticatedRoute"
import Home from './containers/Home'
import Login from './containers/Login'
import Books from './containers/Books'
import NotFound from './containers/NotFound'
export default ({ childProps }) => (
<Switch>
<AuthenticatedRoute path="/" exact component={Home} props={childProps} />
<UnauthenticatedRoute path="/login" exact component={Login} props={childProps} />
<AuthenticatedRoute path="/books" exact component={Books} props={childProps} />
{ /* Finally, catch all unmatched routes */ }
<Route component={NotFound} />
</Switch>
)
和 AuthenticatedRoute 模块,如:
import React from "react";
import { Route, Redirect } from "react-router-dom";
export default ({ component: C, props: cProps, ...rest }) => (
<Route
{...rest}
render={props =>
cProps.isAuthenticated
? <C {...props} {...cProps} />
: (
<Redirect
to="/login"
/>
)}
/>
)
现在,一切正常,但是,当我在/books
路线上并点击浏览器或 上的重新加载按钮时F5,整个应用程序会刷新,并从主页开始,我失去了当前的活动路线。
重新加载时如何保留我的活动路线?
解决方案
推荐阅读
- python - LinearNDInterpolatorExtrapolate 用简单的例子返回错误
- java - 获取异常 java.lang.IllegalStateException: EntityManagerFactory is closed while trying to save secound istance
- c - 如何使用 libpcre2 在纯文本文件中搜索子字符串?
- php - 从 Kartik ExportMenu 小部件导出到 Excel 时,只有在双击它时才会显示换行符
- javascript - 每次登录时如何将url与变量连接起来
- java - 无法使用 Selenium Webdriver 在 Twitter 上的撰写新推文框中输入文本
- django - django 模型 - 避免数据库设计中可能的循环引用
- c - 为什么在这种情况下需要取消对 char 的引用?
- mongodb - 为什么它为0时不显示值?
- java - 数组子集不等和