Reactjs 根据道具状态有条件地重定向用户

Reactjs 根据道具状态有条件地重定向用户,reactjs,redux,react-router,Reactjs,Redux,React Router,我有一个仪表板组件,我试图有条件地重定向用户,这取决于他们是否创建了帐户。如果他们当前没有帐户,则需要将他们重定向到我的CreateAccount组件。如果他们已经有帐户,我想将他们重定向到我的SelectAccount组件 使用下面的代码,我已经拥有帐户的用户将不断被重定向到我的“创建帐户”组件。我有一种感觉,这段代码返回true,因为它在填充道具之前运行检查,但不确定如何修复它 class Dashboard extends React.Component { componentDi

我有一个仪表板组件,我试图有条件地重定向用户,这取决于他们是否创建了帐户。如果他们当前没有帐户,则需要将他们重定向到我的CreateAccount组件。如果他们已经有帐户,我想将他们重定向到我的SelectAccount组件

使用下面的代码,我已经拥有帐户的用户将不断被重定向到我的“创建帐户”组件。我有一种感觉,这段代码返回true,因为它在填充道具之前运行检查,但不确定如何修复它

class Dashboard extends React.Component {
    componentDidUpdate(prevProps) {
      if (prevProps.accounts !== this.props.accounts && (!this.props.activeAccount && this.props.accounts)) {
        if (!_.isEmpty(this.props.accounts)) {
          history.push('/accounts/selectAccount');
        }
      }

      if (prevProps.accounts !== this.props.accounts && (_.isEmpty(this.props.accounts) === true)) {
        if (_.isEmpty(this.props.accounts)) {
          history.push('/accounts/createAccount');
        }
      }
    }
编辑:为了解决问题,我添加了以下代码:

  componentDidUpdate(prevProps, prevState) {
    if (this.props.accounts !== prevProps.accounts && _.size(this.props.accounts) >= 1) {
      console.log("I see some accounts")
    }
    if (this.props.accounts !== prevProps.accounts && _.isEmpty(this.props.accounts)) {
      console.log("There are no accounts")
    }

发生的情况是,对于已经拥有帐户的用户,在渲染的第一次迭代中,它会说“没有帐户”,然后是“我看到一些帐户”。在我的api调用之后,有没有办法让这个逻辑工作?

console.log(prevProps)@MattOestreich所以问题似乎是有两个渲染正在进行。第一次渲染时,对象为空,第二次渲染时,对象中包含项目。我的代码中的问题是,我的条件在第一次渲染时执行,而所有内容都是空的