Skip to main content

5.11 CSS Modules And Media Queries

Person.js
.Person {
width: 60%;
margin: 16px auto;
border: 1px solid #eee;
box-shadow: 0 2px 3px #ccc;
padding: 16px;
text-align: center;
}

@media (min-width: 500px) {
.Person {
width: 450px;
}
}
App.js
...

render() {
let persons = null;
let btnClass = "";

if (this.state.showPersons) {
persons = (
<div>
{this.state.persons.map((person, index) => {
return (
<Person
click={() => this.deletePersonHandler(index)}
name={person.name}
age={person.age}
key={person.id}
changed={(event) => this.nameChangedHandler(event, person.id)}
/>
);
})}
</div>
);

btnClass = classes.Red;
}

const assignedClasses = [];
if (this.state.persons.length <= 2) {
assignedClasses.push(classes.red); // classes = ['red']
}
if (this.state.persons.length <= 1) {
assignedClasses.push(classes.bold); // classes = ['red', 'bold']
}

return (
<div className={classes.App}>
<h1>Hi, I'm a React app!</h1>
<p className={assignedClasses.join(" ")}>This is really working!</p>
<button className={btnClass} onClick={this.togglePersonsHandler}>
Toggle Persons
</button>
{persons}
</div>
);
}
}

export default App;