简体   繁体   English

返回组件时如何重构&&()

[英]How to refactor && () when returning component

I've recently came into a project where the developers are using this unfamiliar syntax where they did something like this: 我最近进入一个项目,开发人员正在使用这种陌生的语法,他们在其中做了如下操作:

    {this.props.something === 'Foo' && (
      <React.Fragment>
       Here
      </React.Fragment>
    )}

How else could I rewrite this? 我还能怎么重写呢?

Due to short circuiting in JavaScript , the whole code basically either evaluates to false if the condition 由于JavaScript中的短路,如果条件满足,整个代码基本上要么评估为false

 this.props.something === 'Foo' 

is not fullfilled, otherwise (if the condition gets fullfilled) it'll evaluate to the following React Component instance. 没有被填充,否则(如果条件被填充)它将评估为以下React Component实例。 As React will filter out falsy values (including the false ) during rendering, this will render the Fragment if something is foo, otherwise it doesn't render anything. 由于React在渲染过程中会过滤掉虚假的值(包括false ),因此如果某物为foo,则将渲染Fragment,否则将不渲染任何东西。

@Jonas Wilms ' answer covers what the syntax means very nicely. @Jonas Wilms的答案很好地说明了语法的含义 However, since you asked specifically about how this could be rewritten, here are some alternative solutions that you might find preferable in certain circumstances. 但是,由于您是专门询问如何重写的,因此这里有一些替代解决方案,在某些情况下可能会更可取。

Supposing you start off with a component like this: 假设您从这样的组件开始:

class MyComp extends Component {
  render() {
    return (
      <div>
        {this.props.something === 'Foo' && (
          <React.Fragment>
            Here
          </React.Fragment>
        )}
      </div>
    );
  }
}

You can convert it to this: 您可以将其转换为:

class MyComp extends Component {
  render() {
    let content;
    if (this.props.something === 'Foo') {
      content = (
        <React.Fragment>
          Here
        </React.Fragment>
      );
    }

    return (
      <div>{content}</div>
    );
  }
}

or this: 或这个:

class MyComp extends Component {
  conditionalContent(something) {
    if (something === 'Foo') {
      return (
        <React.Fragment>
          Here
        </React.Fragment>
      );
    }
  }

  render() {
    return (
      <div>
        {this.conditionalContent(this.props.something)}
      </div>
    );
  }
}

or even this: 甚至这个:

const ConditionalComponent = ({ show }) => {
  if (show) {
    return (
      <React.Fragment>
        Here
      </React.Fragment>
    );
  }
}

class MyComp extends Component {
  render() {
    let content;
    if (this.props.something === 'Foo') {
      content = (
        <React.Fragment>
          Here
        </React.Fragment>
      );
    }

    return (
      <div>
        <ConditionalComponent show={this.props.something === 'Foo'}/>
      </div>
    );
  }
}

Of course, there are many other variations you could come up with. 当然,您可以提出许多其他变体。 This hopefully is enough to get you to think more about how to structure your component for optimum readability. 希望这足以使您思考如何构造组件以实现最佳可读性。

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

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