我可以通过这样的事情:
<CardTitle title={this.props.post.title} subtitle={ <Link to={this.props.post.author}>{this.props.post.author}</Link> } />
但是我需要同时传递一个组件和一些字符串文本,但是这样做不起作用:
<CardTitle title={this.props.post.title} subtitle={(` This is a link: ${<Link to={this.props.post.author}>{this.props.post.author}</Link>} `)} />
正确的语法是什么?
尝试将其完全作为React元素而不是字符串传递:
<CardTitle title={this.props.post.title} subtitle={ <span> This is a link: <Link to={this.props.post.author}>{this.props.post.author}</Link> </span> } />
然后应该能够按subtitle原样渲染。如果您使用的是React> 16,则可能需要为此使用Fragments:
subtitle
import { Fragment } from 'react'; // ... subtitle={ <Fragment> This is a link: <Link to={this.props.post.author}>{this.props.post.author}</Link> </Fragment> }