【发布时间】:2021-12-07 10:24:31
【问题描述】:
我有两个 div,我希望它们在悬停时显示文本,但是当我将鼠标悬停在其中一个上时,它们都显示文本。 代码如下:
> const [text, setText] = useState(0);
> <div onMouseEnter={e=> setText(1)} onMouseLeave={e=> setText(0)}>
> <h1>hover the div to show the text</h1>
> <p style={{opacity: `${text}`}}>some dummy text</p>
> </div>
<div onMouseEnter={e=> setText(1)} onMouseLeave={e=> setText(0)}>
> <h1>hover the div to show the text</h1>
> <p style={{opacity: `${text}`}}>some dummy text</p>
> </div>
【问题讨论】:
-
因为两个div都绑定到同一个状态
text