简体   繁体   English

在ReactJS中重新渲染孩子3次是否可以接受?

[英]Is it acceptable to re-render the children 3 times in ReactJS?

Lets say I have 3 components connected in one parent one. 假设我在一个父组件中连接了3个组件。

Pseudo example: 伪示例:

<div>
 <Parent />
 <ChildMetaData />
 <ChildData />
</div>

On click of <Parent /> I use the following function that's passed as a prop: 单击<Parent />我使用以下作为prop传递的函数:

handleParentClick(tid,sifUser) {
    this.setState({
        tid : tid , 
        sifUser : sifUser
    },
    () => {
        this.loadChildRequestsFromServer();
        this.loadUserDataFromServer();
    });
}

State changes. 国家变化。

=> All 3 components re-render. =>所有3个组件重新渲染。

When my loadChildRequestsFromServer() finishes it changes state of global variable 当我的loadChildRequestsFromServer()完成时,它会改变全局变量的状态

=> All 3 components re-render. =>所有3个组件重新渲染。

When my loadUserDataFromServer() finishes it changes state of global variable 当我的loadUserDataFromServer()完成时,它会改变全局变量的状态

=> All 3 components re-render. =>所有3个组件重新渲染。

As you can see , when I click on my parent my components re-render 3 times. 如您所见,当我点击我的父母时,我的组件重新渲染3次。 ( I check that by running console.log in one of the render function of the component, like <ChildData /> .) (我通过在组件的一个render函数中运行console.log来检查它,比如<ChildData /> 。)

Is this an acceptable behavior , or am I doing something wrong? 这是可接受的行为,还是我做错了什么?

I should note that both of the "load from server" functions are AJAX calls. 我应该注意,“从服务器加载”功能都是AJAX调用。 And since loadChildRequestsFromServer() and this.loadUserDataFromServer() are chained together , maybe I shouldn't immediately update the state after the first function is over. 由于loadChildRequestsFromServer()this.loadUserDataFromServer()被链接在一起,也许我不应该在第一个函数结束后立即更新状态。 But rather pass the data to the next function and update the it there. 而是将数据传递给下一个函数并在那里更新它。

It is perfectly fine for your components to re-render 3 times. 您的组件重新渲染3次是完美的。
Because there are 3 distinct states of your parent: 因为您的父母有3种不同的状态:

  1. without any ajax results 没有任何ajax结果
  2. with ajax results from first call 使用第一次调用的ajax结果
  3. with ajax results from both calls 两个调用都带有ajax结果

React will filter out all unnecessary DOM-updates anyway, which is the main performance bottleneck. React将过滤掉所有不必要的DOM更新,这是主要的性能瓶颈。 You could filter out unnecessary updates by implementing shouldComponentUpdate() . 您可以通过实现shouldComponentUpdate()来过滤掉不必要的更新。 This will also prevent unnecessary executing of render() function. 这也可以防止不必要的render()函数执行。

(update) If the first child only needs first ajax call results, and the second child only needs second call results, then you could also considering giving each child state, and call each function from within its respective child. (更新)如果第一个孩子只需要第一个ajax呼叫结果,而第二个孩子只需要第二个呼叫结果,那么你也可以考虑给每个孩子状态,并从其各自的孩子中调用每个功能。
That way, each child updates independently, and you get less render calls. 这样,每个孩子都可以独立更新,并且您可以获得更少的渲染调用。 Number of DOM updates will remain the same = minimal as compared to the original solution. 与原始解决方案相比,DOM更新的数量将保持不变=最小。

If you NEVER need the state with only results of one ajax call, then you can chain like you describe. 如果您从不需要只有一个ajax调用结果的状态,那么您可以像您描述的那样进行链接。 But that would make any error handling much trickier. 但这会使任何错误处理变得更加棘手。

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

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