1. //下载classnames
    2. yarn add classnames
    1. import React, { Component } from 'react'
    2. import classNames from 'classnames'
    3. import "./index.css"
    4. export default class index extends Component {
    5. state={
    6. isflag:false
    7. }
    8. changeColor=()=>{
    9. this.setState((state,props)=>{
    10. return{
    11. isflag:!state.isflag
    12. }
    13. })
    14. }
    15. render() {
    16. const {isflag}=this.state
    17. return (
    18. <div>
    19. <h1 className={classNames("foo",{"hace":isflag})}>我是一个哈哈哈</h1>
    20. <button onClick={this.changeColor}>改变颜色</button>
    21. </div>
    22. )
    23. }
    24. }
    1. .hace{
    2. color: aqua;
    3. }