【发布时间】:2016-03-06 06:51:30
【问题描述】:
如果我有一个 react 组件并且我想传入一个类名,我该如何使用 CSS 模块来做到这一点。它目前只给出了 className 而不是我会得到的散列生成的 css 模块名称
<div className={styles.tile + ' ' + styles.blue}>
这是我的 Tile.js 组件
import React, { Component } from 'react';
import styles from './Tile.css';
class Tile extends Component {
render() {
return (
<div className={styles.tile + ' ' + this.props.color}>
{this.props.children}
</div>
);
}
};
export default Tile;
Tile.css
@value colors: "../../styles/colors.css";
@value blue, black, red from colors;
.tile {
position: relative;
width: 100%;
padding-bottom: 100%;
}
.black {
background-color: black;
}
.blue {
background-color: blue;
}
.red {
background-color: red;
}
如您所见,我在 Author Tile 中按如下方式初始化此 Tile 包装器组件,但我想将颜色道具传递给组件:
AuthorTile.js
return (
<Tile orientation='blue'>
<p>{this.props.title}</p>
<img src={this.props.image} />
</Tile>
);
【问题讨论】:
标签: css reactjs webpack-style-loader css-modules css-loader