How to pass string and component as prop?

后端 未结 1 1261
感情败类
感情败类 2021-01-19 16:41

I can pass something like this fine:

{this.props.post.auth         


        
相关标签:
1条回答
  • 2021-01-19 17:05

    Try passing it as a React Element altogether, not as a string:

    <CardTitle
      title={this.props.post.title}
      subtitle={
        <span>
          This is a link: <Link to={this.props.post.author}>{this.props.post.author}</Link>
        </span>
      }
    />
    

    Then should be able to render subtitle as is. If you're using React >16, you might want to use Fragments for this:

    import { Fragment } from 'react';
    
    // ...
    
    subtitle={
      <Fragment>
        This is a link: <Link to={this.props.post.author}>{this.props.post.author}</Link>
      </Fragment>
    }
    
    0 讨论(0)
提交回复
热议问题