简体   繁体   English

在 React 上添加和删除(切换)活动 class

[英]Adding and removing (toggle) active class on React

I am changing the state with onClick and adding a border to the data of that state name.我正在用 onClick 更改 state 并为该 state 名称的数据添加边框。 But when I click the other boxes, the previously added border is not removing.但是当我单击其他框时,之前添加的边框不会被删除。 Here is my code;这是我的代码;

    const [toggleState, setToggleState] = useState(null);

  return (
    <React.Fragment>
        <Col sm="6">
        <Card className="text-center">
          <CardBody onClick={() => setToggleState(user.name)} className={toggleState === user.name ? 'selectedItem' : '' + ' cursorPointer'} >
            <Row onClick={event => props.onChange('true')}>
                <Col sm="4">
                </Col>
            </Row>
          </CardBody>
         </Card
         </Col>
       </React.Fragment>

在此处输入图像描述

Hard to say what the className you're looking to set is, but Unmitigated seems to have a good idea about that issue.很难说您要设置的 className 是什么,但 Unmitigated 似乎对此问题有一个好主意。 I also noticed that you don't close your我还注意到你没有关闭你的

</Card

correctly on the third row from the bottom, this might affect how the page behaves as well.正确地位于底部的第三行,这可能也会影响页面的行为方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM