Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在现代前端框架下如何做事件委托? #1

Open
Southhill opened this issue Dec 12, 2019 · 2 comments
Open

在现代前端框架下如何做事件委托? #1

Southhill opened this issue Dec 12, 2019 · 2 comments
Labels
discuss 各抒己见

Comments

@Southhill
Copy link
Owner

ul>li元素的监听事件,通用做法是直接绑定事件到li元素本身,但jQuery时代的最佳实践是通过事件代理的方式绑定事件至li元素的父元素ul。现在的做法是否有本末倒置的嫌疑。

@Southhill Southhill added the discuss 各抒己见 label Dec 12, 2019
@Southhill
Copy link
Owner Author

在React中,"onClick"、"onDoubleClick" 只要是以 on 开头的属性,就是在向react描述期望达成的行为。但react本身不会在底层DOM节点上添加真正的事件处理器。React实际做的是在根元素上添加单个事件处理器,由于事件冒泡机制,这个处理器会监听所有事件。当浏览器触发我们想要的事件时,react会代表相应组件调用处理器,这个技巧称作事件代理,可以优化内存和速度。

@Southhill
Copy link
Owner Author

Southhill commented Jun 17, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss 各抒己见
Projects
None yet
Development

No branches or pull requests

1 participant