css - React 不应用 CSS 规则
问题描述
我正在使用 React 创建一些按钮和 CSS 来设置主题样式。所以我创建了一个类按钮:
export default class Button extends React.Component{
public className: string;
constructor(props){
super(props);
this.className = props.className ? props.className : "";
}
public render() {
return (
<button className = {this.className}>
{this.value}
</button>
);
}
}
这是我的 CSS:
.my-css {
width: 300px;
height: 200px;
border-radius: 8px;
}
这就是我使用它的方式:
<Button className = "my-css" value = "Test" />
为什么在检查器中我没有看到我的规则已加载?
谢谢谁来回答我!:-)
解决方案
render
你应该直接在你的函数中引用你的道具:
export default class Button extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<button className={this.props.className}>
{this.props.value}
</button>
);
}
}
推荐阅读
- jquery - 如何为数据表搜索栏创建特定的 html 结构?
- regex - CMake 正则表达式匹配
- java - 如何将 JSON 对象转换为 DBObject?
- r - 在闪亮的应用程序中传递数据和输入
- spring - Thymeleaf 即时从数据库加载模板
- elasticsearch - 为 2 个映射创建范围的弹性搜索查询
- reactjs - RXJS Observable Epic LOCATION_CHANGE 在受保护的路由重定向之前运行
- curl - 无法使用 Digeest curl 从 url 检索数据
- javascript - 异步迭代器使用的真实示例
- javascript - 如何在异步函数中添加 sequlize.js 事务