【发布时间】:2020-11-20 22:14:29
【问题描述】:
我的按钮只有在我双击它们后才能工作。
我在上一篇文章中了解到,问题在于弹出状态由所有按钮共享,因此如果您单击一个按钮,它会更改每个按钮的状态
谁能帮我找到解决办法?
function Challange() {
const [isPopped, setPop] = useState(false);
const pop = () => {
setPop(!isPopped);
};
return (
<>
{isPopped && <Dialog2 />}
<div className="challanges">
<h1 className="newchallenge">Choose New Challange</h1>
<button className="challangeBtn" onClick={pop}>
Eat Vegetarian (31days)
</button>
<button className="challangeBtn" onClick={pop}>
Take the bike to work (14days)
</button>
<button className="challangeBtn" onClick={pop}>
Recycle your plastic bottles (31days)
</button>
<button className="challangeBtn" onClick={pop}>
Use public transport to commute (31days)
</button>
<button className="challangeBtn" onClick={pop}>
Don't fly an airplane (365days)
</button>
</div>
</>
);
}
export default Challange;
【问题讨论】:
-
我的意思是,如果您交替有 2 个状态,并且您的函数只触发一个,因此需要双击它。
-
我没有看到任何问题。我在您的帖子中添加了可重现的代码。你可以试试。或者也可以添加 Dialog 组件。 codesandbox.io/s/react-playground-forked-q7h8r?file=/…
标签: javascript reactjs