Achieve the multi-selection effect of div (similar to the multi-selection of checkbox, which can be selected or deselected)

export default class App extends React.Component {
  state = {
    expand: flase,
    renList: [],
  }
  handleCheck = value => {
    const { expand, renList } = this.state
    this.setState({ expand: !expand })
    renList.push(value)
  }

  render() {
    return (
      <div>
        {list.map(subItem => (
          <div>
            <div
              className={`${css.sunKind} ${
                this.state.expand === true ? css.active : null
              }`}
              onClick={() => this.handleCheck(subItem)}
            >
              {subItem.name}
            </div>
          </div>
        ))}
      </div>
    )
  }
}

after rendering the content, if you want to click on one of them, it will be selected, then add the class name active to the setting you click, and add it to renList, and then click again to remove the selected state and remove it from renList, similar to the effect of checkbox multi-selection. Now it is somewhat confusing, how do you need to modify it? Thank you very much!

Feb.23,2022

export default class SelectDiv extends Component {
  state = {
    selects: [],
    renList: [{ name: 'n1' }, { name: 'n2' }, { name: 'n3' }],
  }
  handleCheck = ({ name }) => {
    this.setState(({ selects }) => ({
      selects: selects.includes(name)
        ? selects.filter(item => item !== name)
        : [...selects, name],
    }))
  }
  selectAll = () => {
    this.setState(({ renList }) => ({
      selects: renList.map(item => item.name),
    }))
  }
  cancelSelectAll = () => {
    this.setState(() => ({
      selects: [],
    }))
  }
  render() {
    const { selects: selects, renList } = this.state
    return (
      <div>
        <button onClick={this.selectAll}></button>
        <button onClick={this.cancelSelectAll}></button>
        {renList.map(subItem => (
          <div>
            <div
              key={subItem.name}
              className={`${css.sunKind} ${
                selects.includes(subItem.name) ? css.active : ''
              }`}
              onClick={() => this.handleCheck(subItem)}
            >
              {subItem.name}
            </div>
          </div>
        ))}
      </div>
    )
  }
}

generally, it is best to encapsulate list items

Menu