在React JS中的功能组件中单击按钮时有条件地进行渲染

时间:2020-04-14 15:07:11

标签: reactjs routes react-hooks

我有一个立即执行按钮,单击该按钮,检查3个条件(如果条件为真),然后重定向到另一条路线或组件

1 个答案:

答案 0 :(得分:0)

类似的事情应该起作用

onClick={ ()=> {
      if (condition1 && condition2 && condition3) {
         this.setState({redirect : true}); // or setSomeVarible using useState
      }
   } 

}

在渲染中(如果功能正常,则进行类似的检查作为回报)

  if (this.state.redirect) {
     return <Redirect to="/new-route" />
     // or return <NewComponent />
     // or window.location.href = 'https://whatever-url.com'
  }