- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有“mouseover”和“mouseout”事件监听器的组件。几个相同的组件在浏览器中彼此相邻(或重叠)渲染,因此可以按顺序触发“鼠标悬停”、“鼠标悬停”,然后触发另一个“鼠标悬停”事件(如果您将鼠标悬停在从一个元素到下一个元素)。
该组件在所有这些实例中设置状态,但我想知道是否没有更有效的方法来解决此问题,以避免三个状态更新相继发生。
我是否试图在这里进行不必要的优化,或者这是一个合理的担忧吗?这是我的意思的一个例子。在本例中,我只是更新计数,但假设我正在做一些更昂贵的事情,例如迭代数组。
(免责声明,我没有在此处使用新的代码插入,并且在运行此代码段时遇到问题)。
import React, { Component } from 'react';
class DummyComponent extends Component {
state = {
someProp: 1
};
componentDidMount() {
this.addEventListener('mouseover', this.handleEvent);
this.addEventListener('mouseout', this.handleEvent);
}
componentWillUnmount() {
this.removeEventListener('mouseover', this.handleEvent);
this.removeEventListener('mouseout', this.handleEvent);
}
handleEvent(event) {
console.log(event.type);
this.setState({ someProp: this.state.someProp += 1 });
};
render() {
return (
<section>
{this.state.someProp}
</section>
)
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
最佳答案
是否有必要立即处理该事件?如果不是,这似乎是一个很好的用例,用于消除处理程序方法的抖动,以便它的调用频率不会超过 X 毫秒(例如 100 毫秒)。这样做的缺点是处理程序在第一次触发之前将至少等待那么长时间。
Lodash 库提供 debounce 的实现.
以下是如何修改您的代码以使用它:
import React, { Component } from 'react';
import _ from 'lodash';
class DummyComponent extends Component {
state = {
someProp: 1
};
componentDidMount() {
this.addEventListener('mouseover', this.debouncedHandleEvent);
this.addEventListener('mouseout', this.debouncedHandleEvent);
}
componentWillUnmount() {
this.removeEventListener('mouseover', this.debouncedHandleEvent);
this.removeEventListener('mouseout', this.debouncedHandleEvent);
}
handleEvent(event) {
console.log(event.type);
this.setState({ someProp: this.state.someProp += 1 });
};
// Debounced handler with a wait time of 100ms
debouncedHandleEvent = _.debounce(handleEvent, 100)
render() {
return (
<section>
{this.state.someProp}
</section>
)
}
}
关于javascript - react : Batching state updates tied to event listeners,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53625385/
我正在尝试捕捉评论状态事件,例如有人正在关闭它。并在从插件中获得任何生命迹象之后。向日志文件(从任何日志级别)或 STDOUT 发送一行。atlassian-plugins.xml 看起来像这样:
我在跑; sass --watch --style compressed --sourcemap css/sass:css 我得到了; NameError: uninitialized constan
您好,我正在创建一个 android 应用程序作为 ejabbered 服务器的 XMPP 客户端。 但我真的很困惑,因为我看到我可以将消息作为数据包或消息发送,我也可以使用 PacketListen
我有一个 imageview - 它的属性 -focusable 和 focusableintouchmode 都设置为 true 我已经在我的 Activity 中实现了 onFocus
我有一个查询来查找指定条形码的文档 ID: Future findBarcode() async { String searchBarcode = await BarcodeScanner.sca
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 5 年前。 Improve
这可能是一个非常基本的问题,但我就是不明白。使用 Express.js 创建应用和启动应用监听 1234 端口有什么区别,例如: var express = require('express'); v
所以我想知道什么是更好看的解决方案/有什么区别以及在决定制作多个监听器(1 个监听器用于 1 个按钮)还是仅 1 个 ActionListener 用于 GUI 中的所有按钮(大约 10 个按钮),并
我目前正在尝试制作一个聊天室服务器。我正在努力做到这一点,以便我可以同时监听新请求的连接和监听从已建立的连接发送的消息。 我可以用它来监听请求的连接: def reqlisten(): glo
有没有办法在事件监听器方法中访问类上下文并有可能删除监听器? 示例 1: import {EventEmitter} from "events"; export default class Event
应用程序在本地主机上工作正常。但是当它连接到服务器时出现错误。 我通过端口 22 连接服务器 这是错误 Error: listen EADDRNOTAVAIL Error: listen EADDRN
我有类似的东西: $scope.$on(config.SOME_CONSTANT, ()=> { activate(); // plus a bunch of instantiatio
我的 HTML 页面上有以下 Controller : ... ... 此子 Controller 映射到以下 c
我的 HTML 页面上有以下 Controller : ... ... 此子 Controller 映射到以下 c
我构建了一个自定义属性并将其添加到可观察列表中。但是,如果属性内容发生更改,则不会调用任何监听器。以下代码片段向您展示了“建筑”: public static final class TestObje
这里我不明白这两种方法的基本区别是什么。 var events = require('events'); var eventEmitter = new events.EventEmitter(); v
我正在尝试使用 grunt-express 设置 Grunt 来启动我的 Express 服务器。读完docs后和 this SO question ,我还是想不通。我已经为我的 Grunt 文件尝试
如果这里问题的某些方面不清楚,我深表歉意,因为我是 Node 和 javascript 的新手。请询问更多详情 我有一个使用 socketio 连接到 firebase 的 Node 应用程序。在 h
情况 我可能没有使用传统意义上的 PHPUnit。我正在使用带有 Selenium 2 的 PHPUnit。我们有这个想法来记录 Selenium 以“重现步骤”的方式执行的操作。这意味着如果我们调用
我正在尝试学习在 struts2 中使用 session 。所以,我只想实现一个登录/注销、配置文件应用程序。我正在关注互联网上提供的基本教程。但是,它根本不起作用。请帮助我解决以下问题如何解决。 S
我是一名优秀的程序员,十分优秀!