review
npm eject
{ test: /\.css$/, }
to { test: cssRegex, exclude: cssModuleRegex, use: getStyleLoaders({ importLoaders: 1, modules: true, localIdentName: '[name]__[local]__[hash:base64.5]'})
import './App.css;
to import classes from './App.css;
className="App"
to className={classes.App}
; we are no longer calling the classes as strings but as propertiesimport React, {Component} from 'react';
import classes from './App.css';
class App extends Component {
state = {
persons: [ { ... }, { ... } ],
showPersons: false
}
togglePersonsHandler = () => {
const doesShow = this.state.showPersons;
this.setState({showPersons: !doesShow});
}
render() {
const style = {
backgroundColor: 'green',
color: 'white',
font: 'inherit',
border: '1px solid blue',
padding: '8px',
cursor: 'pointer',
}
let persons = null;
if (this.state.showPersons) {
persons = (
<div>
{this.state.persons.map((person, index) => {
return <Person ... />
})}
</div>
);
style.backgroundColor = 'red';
}
let assignedClasses = [];
// NOTE WE NO LONGER PUSH STRINGS I.E. CLASSES.JOIN(' ')
// REFERRING TO THE CSS CLASSES NOT AS STRINGS BUT AS PROPERTIES IN THE CSS
if (this.state.persons.length <= 2) {
assignedClasses.push(classes.red);
}
if (this.state.persons.length <= 1) {
assignedClasses.push(classes.bold);
}
return(
<div className={classes.App}>
<p className={classes.join(' ')}>Setting Class Names Dynamically</p>
<button onClick={this.togglePersonsHandler} style={style}>Toggle Persons</button>
{persons}
</div>
)
}
}
export default App;
import React from 'react';
// no longer works: import './Person.css';
// MUST USE
import classes from './Person.css';
const person = (props) => {
const style = {
// adding media query
'@media (min-width: 500px)': {
width: '450px',
}
};
return (
// CHANGE CLASSNAME ATTRIBUTE
<div className={classes.Person}>
<p onClick={props.switchNameHandler}>Hi! Im {props.name}</p>
</div>
);
}
export default person;
.App button { ... }
.App button:hover { ... }
let btnClass = '';
btnClass = classes.Red;
className={btnClass}