你应该如何有条件地添加像我下面这样的元素?我只要<div className='next-field__connected-wrapper'>
存在于 withFilter
内 Prop 是正确的。
render: function () {
if (this.props.withFilter) {
return (
<div>
<div className='next-card next-card__section next-card__section--no-bottom-spacing'>
<div className='next-field__connected-wrapper'>
{ this.props.children }
</div>
</div>
</div>
)
} else {
return (
<div>
<div className='next-card next-card__section next-card__section--no-bottom-spacing'>
{ this.props.children }
</div>
</div>
)
}
}
最佳答案
请记住,JSX 元素只是 JavaScript 对象,您可以将它们分配给变量并像其他任何元素一样传递它们:
render: function () {
let inner = this.props.children;
if (this.props.withFilter) {
inner = (
<div className='next-field__connected-wrapper'>
{inner}
</div>
);
}
return (
<div>
<div className='next-card next-card__section next-card__section--no-bottom-spacing'>
{inner}
</div>
</div>
);
}
关于javascript - 根据 prop 有条件地添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32596939/