3 回答
TA贡献1817条经验 获得超6个赞
return (
<div className="result-title">
<div className={`header-${removeTitle ? 'without-title' : 'with-title'}`}>{pageTitle}</div>
</div>
);
或使用 https://github.com/JedWatson/classnames
return (
<div className="result-title">
<div className={classNames({ 'header-without-title': removeTitle, 'header-with-title': !removeTitle })}>
{pageTitle}
</div>
</div>
);
编辑:JSX之外的解决方案
const result = (
<div className={classNames({ 'header-without-title': removeTitle, 'header-with-title': !removeTitle })}>
{pageTitle}
</div>
);
return (
<div className="result-title">
{result}
</div>
);
TA贡献1155条经验 获得超0个赞
您可以只内联类名函数
const { pageTitle, removeTitle = false } = props;
const result = classNames({"header-without-title": removeTitle, "header-title": !removeTitle});
return (
<div className="result-title">
<div className={result}>
{pageTitle}
</div>
</div>);
);
TA贡献1995条经验 获得超2个赞
对此有几个答案。取决于每种情况
React 中两个类之间的三元数:
<div className={`header-${removeTitle ? 'without-title' : 'with-title'}`}>
类之间的三元或空在反应Java脚本中:
<div className={removeTitle ? 'without-title' : null}>
渲染类与否在 React Java 脚本和类型脚本中是三元的:
<div className={...(removeTitle ? { className: 'without-title' } : {})}>
添加回答
举报