简体   繁体   English

我应该在哪里将ReactClick组件的onClick逻辑放在哪里?

[英]Where should I position this onClick logic for a ReactJS component?

I have a component that is a button that opens a dialog. 我有一个组件,它是一个打开对话框的按钮。

const myComponent = () => <div><button>click me</button></div>

When the button is pressed I want the dialog to open. 当按下按钮时,我想打开对话框。

Where should I position the onClick functionality (that will dispatch an action against the store to show the dialog)? 我应该在哪里onClick功能(它将对商店调度一个操作以显示对话框)?

Should I use mapStateToProps to create a function on the props that will open the dialog, or is this overkill for such a small component? 我应该使用mapStateToProps在将打开对话框的props上创建一个函数,还是对于这样一个很小的组件来说mapStateToProps过正?

It's depend on many aspects of your design: 这取决于设计的许多方面:

Is it a common button component? 它是常见的按钮组件吗? Do you reuse your button some where else? 您是否在其他地方重复使用按钮?

Then you should pass the onClick event and the caption of the button from props. 然后,您应该传递onClick事件和道具的按钮标题。

Is it a button that only dispatch a function to open the dialog? 它是仅调度一个函数来打开对话框的按钮吗? Is it used to open the dialog only? 它仅用于打开对话框吗?

Then you can handle that inside the component. 然后,您可以在组件内部进行处理。

If it's used to dispatch an action to a store, then use mapDispatchToProps to map to the action to props. 如果用于将动作分派到商店,请使用mapDispatchToProps将动作映射到道具。

In my opinions, no need to use store here unless you have that dialog accessible somewhere else. 我认为,除非您可以在其他地方访问该对话框,否则无需在此处使用存储。

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

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