javascript - 节流 Reactjs 事件系统

标签 javascript jquery performance reactjs google-chrome-devtools

我有一个splitpane我的 React 项目中的组件,在调整 Pane 大小时将对其宽度使用react并更改内容。拆分 Pane 组件具有以下事件监听器...

    componentDidMount: function() {
        document.addEventListener('mouseup', this.onMouseUp);
        document.addEventListener('mousemove', this.onMouseMove);
        window.addEventListener('resize', this.onResize);
    }
    onMouseUp: function() {
    //firing here
    },

我查看了开发工具时间线,事件触发过于频繁,并且页面的性能未达到预期。我怎样才能限制这些事件?

编辑 - 感谢您的快速回复,我想使用计时器而不是引入另一个模块,我知道事实上它不是状态更改,因为它仅在 3 个断点处更改,并且时间线显示事件触发过于频繁(黄色)

我的状态检查 -

shouldComponentUpdate: function(nextProps, nextState) {
        console.log("STATED CHANGED")
        console.log(nextState.showStackingTableState)
        console.log(this.state.showStackingTableState)
        return nextState.showStackingTableState !== this.state.showStackingTableState;
},

更新 - 我想我应该这样做......如果鼠标按下,则触发一个 bool 值,如果 bool 值为真则节流,否则为假。

最佳答案

是事件触发过于频繁,还是您处理事件的方式(例如导致过多的重新渲染)?我打赌是后者,如果是这样,你可以简单地用计时器来限制它,或者使用像 Underscore's throttle 这样的助手。 .

关于javascript - 节流 Reactjs 事件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32868386/

相关文章:

javascript - ReactJS:单独渲染状态而不是作为数组

performance - 在多核机器上扩展多线程应用程序

javascript - 如何播放POST响应中的音频?

javascript - jquery 手机 : set width and height for desktop browser

javascript - 使用js调整div内iframe的大小

jquery - 如何将 2 个 Jquery UI 日期选择器与 Knockout 链接起来?

C# 将一个列表与另一个列表的一部分进行比较

performance - 如何在 AppleScript 的处理程序中有效地构建列表?

javascript - 重置 d3 forceSimulation 中的所有隔离力

jquery - 根据元素的数据属性对 div 进行排序