reactjs - 重定向着陆页组件 5 秒
问题描述
在延迟 5 秒后,我正在尝试使用Redirect
以下Hero React 组件。Switch
React-Router
<Redirect to="/home" />
立即将组件重定向到http://url.com/home。
有关如何处理此问题的任何建议?谢谢。
class Intro extends Component {
render() {
return (
<div className="d-flex w-100 h-100 mx-auto p-4 flex-column">
<Header />
<div
style={{
paddingTop: '40px'
}}>
</div>
<Hero />
<Footer />
</div>
);
}
}
export default Intro;
我也尝试过setTimeout
使用
解决方案
您可以使用注入的道具history
并react-router
调用. 你应该这样做:push
setTimeout
componentDidMount
constructor(props) {
// ...
this.redirectTimeout = null;
}
componentDidMount() {
const { history } = this.props;
this.redirectTimeout = setTimeout(() => {
history.push('/home')
}, 5000);
}
编辑:确保在 5 秒结束前移出页面时清除超时以避免重定向,否则您仍将被重定向:
componentWillUnmount() {
clearTimeout(this.redirectTimeout);
}
推荐阅读
- javascript - 替换 FabricJS 中的对象?
- angular - 将科尔多瓦添加到我的 Angular 应用程序中,它比浏览器中的应用程序慢
- javascript - 在没有 JavaScript 框架的 HTML 中安装 turbolinks
- python - 用于编译网络 (CNN) 的 Keras 自定义损失函数中的错误
- rest - REST 服务 Jersey 2、Guice、HK 和 Message Body Writers and Readers
- c# - 如何通过允许密码中包含非字母数字字符来更改密码策略?
- kubernetes - 如果有现有控制器,如何创建额外的 NGNIX 入口控制器
- python - Python 错误:TypeError:'errstate' 对象不可调用
- c# - 当我执行操作时更新文本框 c#
- sql - 如何在 SQL Server 中将 wgs 84 转换为 lat/long