When working with CSS Modules in a React component, how can I pass a className that includes the hash generated css module name? Currently, it only provides the className itself, like this:
<div className={styles.tile + ' ' + styles.blue}>
Below is the code for my custom Tile.js component:
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;
The corresponding CSS file, Tile.css, looks like this:
@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;
}
To use the Tile component and pass a color prop, you would include it in another component, such as AuthorTile.js:
return (
<Tile orientation='blue'>
<p>{this.props.title}</p>
<img src={this.props.image} />
</Tile>
);