Close previous popup on another button click and open new popup with new data

huangapple go评论61阅读模式
英文:

Close previous popup on another button click and open new popup with new data

问题

我在ReactJs中创建了一个弹出窗口,主页上有多个类别,它们打开相同的弹出窗口组件。如果选择了不同的类别,我想要打开一个新的弹出窗口,并且之前的弹出窗口应该关闭,但是每次单击新类别时都会打开一个新的弹出窗口,覆盖在其他弹出窗口上。

英文:

I made a popup in ReactJs and there are multiple categories on the home page that opens the same popup component. I want to open a new popup if a different category is selected and the previous popup should be closed but a new popup opens over others every time I click on the new category.

答案1

得分: 1

你可以创建一个名为selectedPopup的状态,需要将其初始化为"",然后只有当selectedPopup不等于""时才显示弹出窗口。当你点击一个类别时,只需使用setSelectedPopup("类别名称")来显示弹出窗口,而当你点击关闭时,可以使用setSelectedPopup("")来关闭它。如果用户选择相同的类别,则selectedPopup不会被更新,因此相应的弹出窗口已经显示。

英文:

you can create a state let's call it selectedPopup that you need to initialize to &quot;&quot;, then poup is shown only when selectedPopup is !== &quot;&quot;.<br>when you click on a category you just setSelectedPopup(&quot;category name&quot;) that will show the popup and when you click to close it you do setSelectedPopup(&quot;&quot;).<br>
if the user selects the same category then selectedPopup is not updated so the correspondent popup is already shown.

huangapple
  • 本文由 发表于 2023年7月6日 13:28:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定