我的项目中有多个组件,其中大多数是特定内容的简单容器,带有一些样式。它们通常看起来像这样——
function Portion(props) {
return (
<div id={props.id} className={`portion ${props.className}`}>
{props.children}
</div>
)
}
我有额外的 ${props.className}
,以便在需要时可以轻松添加更多类。现在的问题是,如果该元素没有额外的类,React 会添加一个 undefined
类。
如何避免这种情况?
最佳答案
尝试使用
${props.className || ""}
关于javascript - React向组件添加 "undefined"类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55793476/