简体   繁体   English

如何在reactjs应用程序中创建自定义模式?

[英]How to create custom modal in reactjs app?

I want to make custom confirmation modal whenever user wants to delete his own post. 我想在用户想要删除自己的帖子时制作自定义确认模式。 How to approach that to use as less as possible code? 如何使用尽可能少的代码? I was thinking about independent component with logic inside (user can send via props function on yes/no, etc) but the problem I can't figure out is how to mount this component when user click on a button? 我正在考虑内部具有逻辑的独立组件(用户可以通过道具功能发送是/否等),但我无法弄清楚的问题是当用户点击按钮时如何安装此组件? Do I need to use local state inside every component when I need to use modal? 当我需要使用模态时,是否需要在每个组件中使用本地状态? Something like: showModal ? <Modal onYes={()=>{}} onNo={()=>{}} title='whatever you want' /> : '' 像: showModal ? <Modal onYes={()=>{}} onNo={()=>{}} title='whatever you want' /> : '' showModal ? <Modal onYes={()=>{}} onNo={()=>{}} title='whatever you want' /> : ''

Can I achieve that in other way? 我能以其他方式实现这一目标吗? I hope I explained well. 我希望我解释得很好。

You can use HOC as well. 您也可以使用HOC。 Keep show/hide state inside HOC and then pass props/functions (with currying) from Parent component 在HOC中保持显示/隐藏状态,然后从父组件传递道具/功能(带有currying)

Small example - https://codesandbox.io/s/withtoggle-hoc-8bd0r 小例子 - https://codesandbox.io/s/withtoggle-hoc-8bd0r

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM