javascript - React Router 重定向时的动画过渡
问题描述
我正在探索路线动画和基本示例完美地工作。但是如果在动画 Route 组件中使用重定向,它会因不定式渲染循环而崩溃。
演示:https ://codesandbox.io/embed/v3j736jvwl
转载:按重定向链接
如果我删除 Switch 组件上的位置道具,它可以在没有动画的情况下工作:
<Switch>
<Route exact path="/redirect" component={RedirectToMain} />
<Route exact path="/hsl/:h/:s/:l" component={HSL} />
<Route exact path="/rgb/:r/:g/:b" component={RGB} />
<Route render={() => <div>Not Found</div>} />
</Switch>
我希望页面重定向的动画不会崩溃。
解决方案
您不能使用重定向链接在重定向组件内重定向。而是使用这样的东西:
https://codesandbox.io/embed/v3j736jvwl
class RedirectToMain extends React.Component {
componentDidMount() {
this.props.history.push('/');
}
render () {
return (
<div
style={{
...styles.fill,
...styles.hsl,
background: `red`
}}
>
</div>
);
}
}
推荐阅读
- python-3.x - 复制粘贴不适用于 python selenium 中的无头浏览器
- php - WordPress:编辑器中锚标签中添加的属性会在一段时间后消失,而不会留下来
- wordpress - 如何为所有页面永久禁用 Wp 火箭缓存
- kentico - 使用分层查看器的 Kentico 导航菜单
- file-io - 有什么方法可以为 SUMO 选择一些值以写入输出文件
- react-native - React Native App 设计在不同的 IOS 设备上有所不同
- kentico - Kentico 分层查看器和通用查看器之间的区别
- url - 网址重定向[动态]
- node.js - 我想使用 MongoDB 数据库在 firebase 上部署我的 Node.js 应用程序。但我得到的只是 firebase 欢迎页面,而不是我的网站
- scala - 在 Spark 中将字符串转换为映射