How can I write an else if structure using React (JSX) - the ternary is not expressive enough

前端 未结 6 1671
情话喂你
情话喂你 2021-01-01 18:47

I want to write the equivalent in react:

if (this.props.conditionA) {
    Condition A
} else if (this.props.conditionB) {
    

        
相关标签:
6条回答
  • 2021-01-01 19:13

    If you don't need the <div>, just return the <span> elements:

    render() {
      if (this.props.conditionA) {
        return <span>Condition A</span>;
      } else if (this.props.conditionB) {
        return <span>Condition B</span>;
      } else {
        return <span>Neither</span>;
      }
    }
    

    You can even move the last return statement out of the else block.


    In general, you don't have to embed everything inside JSX. It's perfectly fine to compute values beforehand, just like you do elsewhere:

    render() {
      let content;
      if (this.props.conditionA) {
        content = <span>Condition A</span>;
      } else if (this.props.conditionB) {
        content = <span>Condition B</span>;
      } else {
        content = <span>Neither</span>;
      }
    
      return <div>{content}</div>;
    }
    

    You have to do that whenever you need / want to use a statement.

    0 讨论(0)
  • 2021-01-01 19:15

    Calculating the value, binding to a variable, then outputting later is better. If you do want complex logic inline, you could use && and ||:

    render() {
        return (<div>
            {
              this.props.conditionA && <span>Condition A</span>
              || this.props.conditionB && <span>Condition B</span>
              || <span>Neither</span>
            }
        </div>)
    }
    

    Edit:

    As others pointed out, you can also remove that wrapping div and still use this approach:

    render() {
      return (
        this.props.conditionA && <span>Condition A</span>
        || this.props.conditionB && <span>Condition B</span>
        || <span>Neither</span>
      );
    }
    
    0 讨论(0)
  • 2021-01-01 19:19

    If your condition is as simple as what you expressed, I think you can still use ternary as @SkinnyJ mentioned above. It's quite elegant, but I get your concern if there are lot of these conditions to check. There's one other way to solve this problem: using switch statement.

    const props = {
      conditionA: "this is condition a"
    };
    
    let value;
    
    switch (Object.keys(props)[0]) {
      case "conditionA":
        value = "Condition A";
        break;
      case "conditionB":
        value = "Condition B";
        break;
      default:
        value = "Neither";
    }
    
    console.log(value);
    

    There are a couple of assumptions being made here. That the object is not null and that it has only one property.

    But if those are true, for scenarios like this, switch might be more performant. This might be of interest for you:

    Javascript switch vs if else

    0 讨论(0)
  • 2021-01-01 19:26

    Why do you say that the ternary is not expressive enough?

    render() {
      return <span>
        {this.props.conditionA ? "Condition A" 
          : this.props.conditionB ? "Condition B" 
          : "Neither"}
      </span>;
    }
    
    0 讨论(0)
  • 2021-01-01 19:27

    If any one still facing this issue, please paste below line in your eslintrc.js file.

    "no-nested-ternary" : "off" 
    

    This will allow you to start using nested ternary in your jsx code.

    0 讨论(0)
  • 2021-01-01 19:33

    Indeed, that is not the way.

    var element;
    if (this.props.conditionA) {
        element = (<span>Condition A</span>)
    } else if (this.props.conditionB) {
        element = (<span>Condition B</span>)
    } else {
        element = (<span>Neither</span>)
    } 
    ...
        {element}
    
    0 讨论(0)
提交回复
热议问题