简体   繁体   English

我应该在React JS中使用自定义事件委托

[英]Should I use custom event delegation in React JS

React uses event delegation, as mentioned in the documentation here : 反应使用事件代表团,如文档中提到的在这里

Event delegation: React doesn't actually attach event handlers to the nodes themselves. 事件委托:React实际上并不将事件处理程序附加到节点本身。 When React starts up, it starts listening for all events at the top level using a single event listener. 当React启动时,它会使用单个事件侦听器开始侦听顶级的所有事件。 When a component is mounted or unmounted, the event handlers are simply added or removed from an internal mapping. 安装或卸载组件时,只需在内部映射中添加或删除事件处理程序。

I have a very common scenario where I have a list of items and I want a event handler on each item, should I use my custom event delegation and access target element from event object to perform logic or should I attach individual event listener callbacks to each list item and rely on React to take care of performance. 我有一个非常常见的场景,我有一个项目列表,我想在每个项目上有一个事件处理程序,我应该使用自定义事件委托和访问事件对象中的目标元素来执行逻辑,还是应该为每个项目附加单独的事件监听器回调列出项目并依靠React来处理性能。

Attach event handler to each. 将事件处理程序附加到每个 You might look into paging the list, most displays won't show 500-1000 items at a time. 您可能会查看分页列表,大多数显示器一次不会显示500-1000个项目。

 class SnipListItemRender extends React.Component { render() { let SnipSpanSty = {width: 'calc(70% - 142px)'}; SnipSpanSty.color = (this.props.index === this.props.selectedKey) ? '#b58900' : '#afac87'; return ( <div id='SnipDivSty' onclick={this.snipClickHandler} className="FlexBox" style={SnipDivSty}> <div id='SelectSnipDivSty' style={SelectSnipDivSty}> <JButton btn={selectSnipBtn} parentClickHandler={this.snipClickHandler} /> </div> <span id='SnipSpanSty' style={SnipSpanSty}>{this.props.snippet.snip}</span> <JButton btn={SnipBtn} parentClickHandler={this.snipClickHandler} /> </div> ); } } class SnipListItem extends SnipListItemRender { snipClickHandler = (buttonid) => { Actions.selectSnipItem(this.props.snippet, buttonid); } } let _snipDataMap = function(snip, index) { return ( <li id='SnipsDivLiSty' key={index} style={SnipsDivLiSty}> <SnipListItem snippet={snip} index={index} selectedKey={this.props.selectedKey} /> </li> ); } export default class SnipsView extends React.Component { render() { let list = this.props.data.map(_snipDataMap, this) return ( <div id='SnipsDivSty' style={SnipsDivSty}> <ul id='SnipsDivUlSty' style={SnipsDivUlSty}> {list} </ul> </div> ); } } 

If your custom event delegation means having custom data in event, you shouldn't do it. 如果您的custom event delegation意味着在事件中拥有自定义数据,则不应该这样做。 Because it against React's philosophy. 因为它违背了React的理念。

  • New data flow is introduced 引入了新的数据流
    • React prefer one-way data flow React更喜欢单向数据流
  • Implicit dependency is introduced 引入了隐含依赖
    • React prefer passing data and functions to props. React更喜欢将数据和函数传递给props。 All dependencies should be explicit 所有依赖项都应该是显式的

For more details, you can read this 有关详细信息,请阅读此内容

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

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