javascript - history.push 每次都不起作用
问题描述
我正在制作一个 ReactJS 项目,在登录页面中,我添加了当用户登录成功时,他会自动重定向到网站主页的功能。身份验证是通过 Firebase 进行的,这是代码
auth.signInWithEmailAndPassword(email, password)
.then(() => {
console.log('Successfully signed in');
this.setState({
isLoading : false
});
this.props.history.push('/main');
})
.catch(err => {
alert("There was some error signing in");
console.log(err);
})
但是,history.push 并非每次都有效。有时当我登录时,它会重定向到所需的页面,但有时不会。
请告诉发生了什么问题以及如何解决
解决方案
我在后端使用 mongodb 时遇到了同样的问题。有时我可以在历史推送后看到更新的值,但有时没有。我通过在调用后端路由的 onSubmit 方法中使用超时值解决了这个问题。
setTimeout(() => { history.push("/") }, 10);
推荐阅读
- javascript - 如何在触摸设备中使用 javascript 在固定容器内水平滚动图像?
- sql - Oracle SQL:将每个 CSV 值作为一行返回
- php - Wordpress functions.php 不会使用 unset() PHP 函数保存
- api - Rest api开发:出现错误:找不到媒体类型=应用程序/xml的MessageBodyWriter
- java - 使用 antlr4 检查 java 代码中的用户输入
- android - 是否可以通过 ./gradlew 命令传递构建配置参数?
- c# - 在 ASP.Net Framwork API 中配置和使用 AutoMapper
- grafana - 使用 Grafana 离散面板监控标签 (InfluxDB)
- c++ - 二叉树到循环双向链表
- python-3.x - 仅使用 Opencv python 裁剪前额