英文:
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 ""
, then poup is shown only when selectedPopup
is !== ""
.<br>when you click on a category you just setSelectedPopup("category name")
that will show the popup and when you click to close it you do setSelectedPopup("")
.<br>
if the user selects the same category then selectedPopup
is not updated so the correspondent popup is already shown.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论