【问题标题】:How to reuse JSX elements in react?如何在 React 中重用 JSX 元素?
【发布时间】:2018-04-15 10:42:32
【问题描述】:

我不知道将 span 中的代码和附加它的文本分开

getAuthorInfo() {
    if (this.props.anonymous_author) {
        return <p className="meta-data-type topic-label">{this.props.author.display_name}, {this.props.author.primary_specialty_name}</p>;
    }
    return <p className="meta-data-type topic-label">{this.props.author.display_name}, {this.props.author.primary_specialty_name} <span className="glyphicon glyphicon-map-marker" />{this.props.author.location}</p>;
}

我认为 jsx 不允许我将变量定义为纯文本 有什么想法吗?

【问题讨论】:

  • 不太明白你的问题
  • 你在使用 React 16 吗?

标签: javascript html reactjs jsx


【解决方案1】:
getAuthorInfo() {
  return (<p className="meta-data-type topic-label">
      {this.props.author.display_name}, {this.props.author.primary_specialty_name}
      {!this.props.anonymous_author &&
        (<span className="glyphicon glyphicon-map-marker" />
        {this.props.author.location})}
    </p>);    
}

【讨论】:

  • 虽然这比原来的要好,但它仍然存在不可跨组件重用的问题
  • 它不能重复使用,但至少没有块重复。
  • 我可以从你的帖子中看到语法错误,甚至不需要复制粘贴到 IDE....
【解决方案2】:

我最终得到了这个:

getAuthorInfo() {
    let location;
    if (!this.props.anonymous_author) {
        location = (<span><span className="glyphicon glyphicon-map-marker" />{this.props.author.location}</span>);
    }
    return <p className="meta-data-type topic-label">Top answer from {this.props.author.display_name}, {this.props.author.primary_specialty_name} {location} </p>;
}

关键是我用另一个跨度包装了跨度+文本。它在 html 中是允许的,请参见此处: https://stackoverflow.com/a/5545914/5326788

【讨论】:

    【解决方案3】:

    你想错了。您不应该从这样的函数中返回 JSX,因为它不可重用并且会遇到您现在遇到的问题。

    这里的所有其他答案仍然不可测试或跨组件重复使用。

    目前我不知道你是如何渲染的,但假设你是这样渲染的:

    class MyComponent extends React.Component {
        getAuthorInfo() {
            if (this.props.anonymous_author) {
                return <p className="meta-data-type topic-label">{this.props.author.display_name}, {this.props.author.primary_specialty_name}</p>;
            }
            return <p className="meta-data-type topic-label">{this.props.author.display_name}, {this.props.author.primary_specialty_name} <span className="glyphicon glyphicon-map-marker" />{this.props.author.location}</p>;
        }
      render() {
        return (
            <div>
                {this.getAuthorInfo()}
            </div>
        );
      }
    }
    

    变成这样:

    const MyComponent = () => <AuthorInfo />;
    
    const AuthorInfo = (props) => (
      <p className="meta-data-type topic-label">
        {props.author.display_name}, {this.props.author.primary_specialty_name}
    
        {!props.anonymous_author ? <AnonymousAuthor author={props.author} /> : null}
      </p>
    );
    
    const AnonymousAuthor = (props) => (
      <div>
        <span className="glyphicon glyphicon-map-marker" />
        {props.author.location}
      </div>
    );
    

    你获得智能感知。它是可重复使用的,您可以导出每个组件以轻松进行单独测试。如果您不想使用三元运算符,则可以使用 recompose branch() 函数,但这更高级。

    【讨论】:

    • “这里的所有其他答案仍然不可测试或跨组件重用。”这完全不是真的,这个说法是-1
    • @WilliamB 您如何在其他组件中重用 OP 的 getAuthorInfo?除了字符串 concat answer 之外,它们实际上是可测试的
    • 模块化和代码重用在 React 之前就已经存在。与您共享任何其他逻辑、模块、对象等的方式相同。
    • @WilliamB 当然。如果你想学究气,你可以重复使用它,但这并不好,这是我试图传达给 OP 的。 React 的一大特点是将其拆分为不同的组件以便于重用。然后,您必须将该代码拆分为一个没有意义的共享函数。
    • 但是“仅仅因为你可以排除某些东西并不意味着你必须排除它。”说返回 JSX 的方法应该始终是单独的组件是不正确的。这方面有很多经验法则,过度组件化所有渲染逻辑实际上将是 IMO 的糟糕开发体验。 reactarmory.com/answers/how-should-i-separate-components
    猜你喜欢
    • 1970-01-01
    • 2019-03-20
    • 2016-06-16
    • 2021-10-25
    • 2019-10-19
    • 2018-03-07
    • 2021-06-16
    • 2019-03-10
    • 2018-08-24
    相关资源
    最近更新 更多