[英]Adding and removing (toggle) active class on React
我正在用 onClick 更改 state 并为该 state 名称的数据添加边框。 但是当我单击其他框时,之前添加的边框不会被删除。 这是我的代码;
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>
很难说您要设置的 className 是什么,但 Unmitigated 似乎对此问题有一个好主意。 我还注意到你没有关闭你的
</Card
正确地位于底部的第三行,这可能也会影响页面的行为方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.