简体   繁体   English

反应。 条件渲染

[英]React. Conditional Rendering

I have a React component:我有一个 React 组件:

class SignUpStepTwo extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isTherapistUpdated: false,
      isProfessionalInfoAdded: false,
    }
  }

  render() {

    if (!this.state.isTherapistUpdated) {
      return (
        <StepTwoPersonalInfo />
      );
    }
    else if (this.state.isTherapistUpdated) {
      return (
        <StepTwoProfessionalInfo />
      );
    }
    else if (this.state.isProfessionalInfoAdded &&
      this.state.isTherapistUpdated) {
      return (
        <StepTwoTermsOfUse />
      );
    }
  }
}

export default SignUpStepTwo;

And I'm trying to change my bool flags and conditionally render my components.我正在尝试更改我的 bool 标志并有条件地呈现我的组件。 But the last if is never satisfied and the <StepTwoTermsOfUse/> component is not rendered.但是最后一个if永远不会满足并且<StepTwoTermsOfUse/>组件不会呈现。

It's because the second condition is satisfied even if both this.state.isProfessionalInfoAdded and isTherapistUpdated are true.这是因为即使 this.state.isProfessionalInfoAdded 和 isTherapistUpdated 都为真,第二个条件也得到满足。

To make this work you should put the third condition, before the second.为了使这项工作有效,您应该将第三个条件放在第二个条件之前。 Also, I've restructured your code a bit to skip unnecessary checks:另外,我对您的代码进行了一些重组以跳过不必要的检查:

if (this.state.isProfessionalInfoAdded && this.state.isTherapistUpdated) {
  return (
      <StepTwoTermsOfUse/>
  );
}

if (this.state.isTherapistUpdated) {
  return (
      <StepTwoProfessionalInfo/>
  );
}

return (
    <StepTwoPersonalInfo />
);

你应该把最后一个如果放在第一个,它应该可以工作!

You should switch your last two statements.您应该切换最后两个语句。 The second if statement in your question satisfies if the 3rd one satisfies as well.如果第三个也满足,则问题中的第二个 if 语句也满足。 So the seconds one make the function exit.所以秒一使函数退出。

class SignUpStepTwo extends Component {
constructor(props) {
        super(props);
        this.state = {
            isTherapistUpdated: false,
            isProfessionalInfoAdded: false,
            }
        }
     }
render() {
        if (!this.state.isTherapistUpdated) {
            return (
                <StepTwoPersonalInfo />
            );
        }
        if (this.state.isProfessionalInfoAdded) {
            return (
                <StepTwoTermsOfUse/>
            );
        }

        return (
            <StepTwoProfessionalInfo/>
        );
    }
}

export default SignUpStepTwo;

What this does is:它的作用是:

  • If isTherapistIsUpdated === false , then render StepTwoPersonalInfo如果isTherapistIsUpdated === false ,则渲染StepTwoPersonalInfo
  • If isTherapistIsUpdate === true (implied, because the first if does not match) AND isProfessionalInfoAdded === true , then render StepTwoTermsOfUse如果isTherapistIsUpdate === true (暗示,因为第一个 if 不匹配) AND isProfessionalInfoAdded === true ,则渲染StepTwoTermsOfUse
  • In any other case, which means isTherapistIsUpdated === true AND isProfessionalInfoAdded === false , render StepTwoProfessionalInfo在任何其他情况下,这意味着isTherapistIsUpdated === true AND isProfessionalInfoAdded === false ,呈现StepTwoProfessionalInfo

I think if you use a temporary variable to set your logic result to, the code resuls being a bit clearer...我想如果你使用一个临时变量来设置你的逻辑结果,代码结果会更清晰一些......

class SignUpStepTwo extends Component {

    constructor(props) {
        super(props);
        this.state = {
            isTherapistUpdated: false,
            isProfessionalInfoAdded: false,
        }
    }

    render() {
        let render = (
          (!this.state.isTherapistUpdated) ? <StepTwoPersonalInfo /> :
          (this.state.isTherapistUpdated && this.state.isProfessionalInfoAdded) ? <StepTwoTermsOfUse/> :
          <StepTwoProfessionalInfo />
        );
        return render;
    }
}

export default SignUpStepTwo;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM