【发布时间】:2019-06-20 00:15:45
【问题描述】:
我想知道如何向自定义 React 组件(如 Button)添加额外的类名。到目前为止,我已经尝试过使用class1 ${class2} 甚至classnames 的传统方式,但没有成功。
这是我的尝试示例: https://codesandbox.io/s/1zp7m2yv3
import React from "react";
import ReactDOM from "react-dom";
import classNames from "classnames";
import "./styles.css";
const ButtonTest = props => {
// const className = `button is-testing is-gradient ${props.className ? props.className : ''}`;
const className = classNames(
"button is-testing is-gradient cv",
props.className
);
console.log("a:", className);
if (props.href) {
return (
<a href={props.href} className={className} {...props}>
{props.children}
</a>
);
}
return (
<button className={className} {...props}>
{props.children}
</button>
);
};
function App() {
return (
<div className="App">
<ButtonTest href="https://google.vn">BUTTON LINK</ButtonTest>
<ButtonTest className="aaa is-block">DEFAULT</ButtonTest>
<ButtonTest href="https://google.vn" className="is-block dfdfdf">
BUTTON LINk - FULL
</ButtonTest>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
谢谢, D
【问题讨论】:
-
您能否详细描述您的问题?
-
问题是当我尝试添加更多类时,似乎定义的类
button is-testing is-gradient cv被更新的类替换,我的示例中的预期结果是所有 Button 组件都应该有一个边框,因为 CSS 中的.is-testing
标签: css reactjs components custom-component