简体   繁体   English

如何在反应中重用JSX元素?

[英]How to reuse JSX elements in react?

I don't know separate the code in span and the text appending it 我不知道跨度的代码和附加它的文本

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>;
}

I don't think jsx let me to define a variable to be just and raw text any ideas? 我不认为jsx允许我将变量定义为公正,而将原始文本定义为任何想法?

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>);    
}

I ended up with this: 我结束了这个:

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>;
}

The key point is I wrap the span+text with another span. 关键是我将span + text换成了另一个span。 And it's allowed in html, see here: https://stackoverflow.com/a/5545914/5326788 而且它是html允许的,请参见此处: https : //stackoverflow.com/a/5545914/5326788

You are thinking about this the wrong way. 您在想这是错误的方式。 You shouldn't be returning JSX from functions like this because it's not re-usable and runs into the problems which you are having right now. 您不应该从这样的函数中返回JSX,因为它不可重用并且会遇到您现在遇到的问题。

All the other answers here are still not testable or re-usable across components. 此处的所有其他答案仍无法在组件之间进行测试或重用。

I don't know how you render at the moment, but let's say you render it like this: 我目前不知道您如何渲染,但可以说您是这样渲染的:

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>
    );
  }
}

Becomes this: 变成这个:

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>
);

You gain intellisense. 您将获得智能感知。 It's re-usable and you can export each component to test easily in isolation. 它是可重复使用的,您可以导出每个组件以独立进行测试。 If you don't want to use ternary operators then you can use recompose branch() function but this is more advanced. 如果您不想使用三元运算符,则可以使用recompose branch()函数,但这是更高级的。

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

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