简体   繁体   English

React JS,如何仅允许组件每秒更新一次

[英]React JS, How to only allow component to update once per second

I am having trouble limiting how often a component gets updated. 我在限制更新组件的频率方面遇到麻烦。 Component A has a parent Component B. 组件A具有父组件B。

I want to have Component A only update once per second, as we are using socket IO to update live results, but when there are 100's of users we get too many updates and the page renders way more than we'd like giving us a glitchy look as a bar graph goes up and down very quickly. 我想让组件A每秒仅更新一次,因为我们正在使用套接字IO更新实时结果,但是当有100个用户时,我们得到的更新太多了,页面呈现的方式超出了我们希望给我们带来的故障条形图看起来非常迅速地上升和下降。

Is there a way to ONLY allow Component A to update once per second? 有没有办法只允许组件A每秒更新一次? Do we have to control how often Component B passes down props to Component A? 我们是否必须控制组件B多长时间传递一次道具到组件A?

What you're looking is for throttling , which would allow a function to run at most once per second per your requirement. 您正在寻找的是节流 ,这将使一个功能根据您的需求最多每秒运行一次。

Where should I throttle? 我应该在哪里节流?

The principle is to throttle whatever that will trigger a mutation on those states in the container component (the component that hosts the states that become props to the presentation component), not to throttle the rendering itself. 原理是限制将在容器组件(承载成为表示组件的状态的组件的组件)中的那些状态上引发突变的所有内容,而不是限制渲染本身。

If I throttle in the presentation component, now all the views I desire to be throttled need to change into "throttled components", and these can no longer be pure functional presentation components. 如果我在表示组件中进行限制,那么现在我希望被限制的所有视图都需要更改为“限制的组件”,而这些视图不再可以是纯粹的功能性呈现组件。

Reusable components for presentation don't need to, or even must not know about throttling. 用于演示的可重用组件不需要,甚至不必知道节流。 The containers that use them decide on throttling and other behavior. 使用它们的容器决定节流和其他行为。 Throttling is only required because of the real time feed, so it should live where the feed is processed by the container, and be limited there without impacting the structure of the rest of the app. 仅由于实时提要才需要节流,因此它应该存在于容器处理提要的位置,并在不限制应用程序其余部分结构的情况下进行限制。

By following this principle, throttling can be disabled or modified from a single place. 通过遵循此原理,可以从单个位置禁用或修改节流。 And there will be no unnecessary state mutations in the container that will end up being throttled by the "throttled components" anyway. 而且,容器中不会出现不必要的状态突变,无论如何,这些突变最终都会被“节流的组件”节流。

Less important implementation details 不太重要的实施细节

You can implement it yourself, or use a library like Lodash that implements throttling (among other things). 您可以自己实现它,也可以使用Lodash之类的库来实现节流(以及其他功能)。

You can use throttle to throttle the state updates that would trigger a render on Component A 您可以使用throttle来限制将触发Component A上的渲染的状态更新Component A

_.throttle(func, [wait=0], [options={}]) _.throttle(func,[wait = 0],[options = {}])

Creates a throttled function that only invokes func at most once per every wait milliseconds. 创建一个受限制的函数,每等待一个毫秒最多只能调用一次func。

So for you func would be the function that would trigger a state update, and wait would be 1000. (1000 ms is 1 second) 因此,对您来说func是触发状态更新的函数, wait时间为1000。(1000毫秒为1秒)

You can use shouldComponentUpdate and store the last update time in a variable then comparing now with the last updated time. 您可以使用shouldComponentUpdate并将上次更新时间存储在变量中,然后将其与上次更新时间进行比较。

class MyComponent extends Component {
    constructor() {
        this.lastUpdateDate = new Date();
    }

    shouldComponentUpdate() {   
        const now = new Date();  
        var seconds = (now.getTime() - this.lastUpdateDate.getTime()) / 1000;   
        return seconds >= 1;   
    }

    componentDidUpdate() {     
        this.lastUpdateDate = new Date();  
    } 

    render() {
        return <span>My Component</span>;
    }
}

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

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