javascript - ReactJS,渲染后重定向

标签 javascript reactjs redirect react-router higher-order-functions

有没有办法或者如何在一些return之后重定向上render

我想要做的是这样的事情:

render() {
  return(<div>You have no access</div>)
  // then redirect to "/"

我正在考虑创建一个仅在 componentDidMount 中具有重定向的组件3秒后,例如调用<Redirect>然后像这样使用它:

render() {
  return(<Redirect time=3> <div>You have no access</div> </Redirect>
  // then redirect to "/"

重定向.js

componentDidMount() {
  // code for redirect using with this.props.time

还有别的办法吗?

最佳答案

是的,您可以实现这种重定向。

由于路由时可以传递数据,因此可以将上一个URL传递给该组件,以替换/,实现返回上一个页面

componentDidMount() {
  const intervalId = setInterval(this.timer, 1500);
  this.setState({ intervalId: intervalId });
}
componentWillUnmount() {
  clearInterval(this.state.intervalId);
}
timer = () => {
  this.props.history.push("/");
};

剩下的唯一事情就是根据代码需求将其设为通用组件,或者用于auth检查的HOC。

<小时/>

enter image description here

在线尝试:

Edit cranky-chatelet-quoy7

关于javascript - ReactJS,渲染后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60761108/

相关文章:

javascript - 在 react 中安装后如何设置元素的状态?

javascript - 在 React 中将 props 传递给 child

javascript - Node : node-http-proxy and harmon: rewriting the html response from the end point instead of the 302 redirected response.

php - 显示不带扩展名的正确 URL,如果 URL 不正确则重定向

javascript - AngularJs 属性指令 2 路绑定(bind)

javascript - 单击动态列表创建的元素并显示动态内容

javascript - 使用 react-konva 捕捉网格,拖放

powershell - Java keytool.exe powershell 包装器不会抑制标准错误

javascript - 如何在 Flux Store 中对状态变量实现隐私保护?

javascript - Fabric.js 动画对象/图像