[英]React-Router v5 not redirecting if accessing specific route using history.push
In my react app I have /lead-manager
route, /login
route and there is /
route, latter which is the app.js
itself that I don't want anyone to access, if someone just goes to the /
route I wanted them to redirect to /lead-manager
route.在我的反应应用程序中,我有
/lead-manager
路由、 /login
路由和/
路由,后者是我不希望任何人访问的app.js
本身,如果有人只是去我想要的/
路由重定向到/lead-manager
路由。 So this is what I did according to the solution given here .所以这就是我根据这里给出的解决方案所做的。
My app.jsx part我的 app.jsx 部分
import {
BrowserRouter as Switch,
useHistory,
Route,
useLocation,
} from "react-router-dom";
const App = ({ confirmAuthentication }) => {
let location = useLocation();
let history = useHistory();
useEffect(() => {
async function fetchIt() {
if (localStorage.jwt !== undefined) {
confirmAuthentication();
}
}
fetchIt();
if (location.pathname === "/") {
history.push("lead-manager");
}
}, []);
return (
<div>
<Switch>
<Route exact path="/testing">
<TestingComponent />
</Route>
<PrivateRoute path="/lead-manager" component={LeadApp} />
<Route exact path="/login">
<LoginView />
</Route>
</Switch>
</div>
);
}
The output I'm getting when I'm accessing /
path is /lead-manager
in the URL.我在访问
/
路径时得到的 output 是 URL 中的/lead-manager
。 Which is the desired one, but the component doesn't render accordingly with components defined in /lead-manager
route.这是所需的,但该组件不会与
/lead-manager
路由中定义的组件相应地呈现。 It just stays on /
route even though URL shows /lead-manager
即使 URL 显示
/
/lead-manager
Hope this is clear希望这很清楚
In order to use history correctly you need to have a Router wrapping App component up in the hierarchy.为了正确使用历史记录,您需要在层次结构中使用路由器包装 App 组件。 Since you are rendering Router within App component, it isn't able to listen to the history changes since the history provided by useHistory doesn't refer to the history used by Router unless App component is wrapped by Router.
由于您在 App 组件中渲染路由器,因此它无法监听历史更改,因为 useHistory 提供的历史不引用路由器使用的历史,除非 App 组件由路由器包装。
Do not render Router inside of App component不要在 App 组件内渲染路由器
Also you have rendered Router as the Switch component inside App component, you must instead import the exact Switch component from react-router-dom
此外,您已将 Router 渲染为 App 组件内的 Switch 组件,您必须改为从
react-router-dom
导入确切的 Switch 组件
Check updated code below检查下面的更新代码
import {
BrowserRouter as Router,
Switch
useHistory,
Route,
useLocation,
} from "react-router-dom";
const App = ({ confirmAuthentication }) => {
let location = useLocation();
let history = useHistory();
useEffect(() => {
async function fetchIt() {
if (localStorage.jwt !== undefined) {
confirmAuthentication();
}
}
fetchIt();
if (location.pathname === "/") {
history.push("lead-manager");
}
}, []);
return (
<div>
<Switch>
<Route exact path="/testing">
<TestingComponent />
</Route>
<PrivateRoute path="/lead-manager" component={LeadApp} />
<Route exact path="/login">
<LoginView />
</Route>
</Switch>
</div>
);
}
export default (props) => <Router><App {...props}/></Router>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.