我使用react-router-dom来帮助处理路由,但是在用户的身份验证状态更改后,当我尝试切换到"/“路由(我的主页)时,它不会触发重定向。路由在整个应用程序中以其他方式工作(通过链接)。应用程序在整个过程中都正确地反映了用户状态,只是没有重定向。
我的SignUp组件是一个类,而不是功能组件,所以我不能使用useHistory这样的钩子。
这是我的App.tsx:
componentDidMount() {
auth.onAuthStateChanged((user) => {
this.setState({ currentUser: user }, () => {
console.log("this is running")
// up to here is in fact being called when there is a successful sign in
return <Redirect to="/" />;
});
});
}
render() {
return (
<div>
<Header currentUser={this.state.currentUser} />
<Switch>
<Route path="/" exact={true}>
<Home currentUser={this.state.currentUser} key={`home`} />
</Route>
<Route
exact={true}
path="/sign-up"
component={SignUp}
key={`sign-up`}
/>
</Switch>
</div>
);
}
}发布于 2020-09-06 03:22:55
尝试使用以下命令:
<Redirect
to={{
pathname: "/",
state: { from: location }
}}
/>发布于 2020-09-06 05:18:36
我更喜欢函数组件,但是你会在auth修改时返回一个标签,对吧?这有什么用吗?我认为只有在路由被触发后,<Redirect>标签才能在<Router>中起作用。
也许你可以在这里找到一些东西:How to push to History in React Router v4?
https://stackoverflow.com/questions/63757646
复制相似问题