简体   繁体   English

调试反应组件重新渲染

[英]Debug react component rerender

I'm trying to debug bug that is reproduced very rarely (once per several dozens of trys).我正在尝试调试很少重现的错误(每几十次尝试一次)。

I'm inside of render call and want to find out as much information as possible from there:我在render call 里面,想从那里找到尽可能多的信息:

  • What caused rerender: forceUpdate , setState or props changed?是什么导致了 rerender: forceUpdatesetStateprops的变化?
  • If it was a props change, which component initiated chain of rerenders: I mean not the fact of parent render, but the top component from parents chain that has used setState or forceUpdate and so caused rerender of itself and the whole tree bellow (including the component I'm currently in).如果是props更改,哪个组件启动了重新渲染链:我的意思不是父级渲染的事实,而是父级链中使用setStateforceUpdate的顶级组件,因此导致自身和整个树的重新渲染(包括我目前所在的组件)。

It's guaranteed that all components are class components (not functional) and I'm using react 16.4.2 including deprecated lifecycle methods (if it is important).保证所有组件都是 class 组件(不起作用),并且我正在使用 react 16.4.2 包括已弃用的生命周期方法(如果它很重要)。 There are some usages of context too.上下文也有一些用法。

How can I make it?我怎样才能做到? Note, I'm asking about debug techniques, not about fixing my concrete bug.请注意,我问的是调试技术,而不是修复我的具体错误。

If your component is a pure component - extends React.PureComponent {... } , you don't really have to care about the whole chain of re-renders, since it will re-render only if props have changed (from the chain or from the redux store).如果您的组件是纯组件 - extends React.PureComponent {... } ,您实际上不必关心整个重新渲染链,因为它只会在道具发生变化时重新渲染(来自链或从 redux 商店)。

It will not re-render if parent state changed or if parent basically has re-rendered.如果父级 state 更改或父级基本上已重新渲染,则不会重新渲染。

However , it will still re-render - as I mentioned above - if props have changed or state inside that component was updated or forceUpdate function was called.但是,它仍然会重新渲染 - 正如我上面提到的 - 如果道具已更改该组件的 state 已更新或forceUpdate function 被调用。

If you are not calling forceUpdate inside your component or you are not updating the state, the issue may be related with the props.如果您没有在组件内调用forceUpdate或者您没有更新 state,则问题可能与道具有关。

You could use eg componentDidUpdate and simply compare the prevProps with this.props and check which one of the props has changed.您可以使用例如componentDidUpdate并简单地将prevPropsthis.props进行比较,并检查哪一个道具已更改。

Nonetheless, you can also check https://www.npmjs.com/package/@welldone-software/why-did-you-render package, it might be helpful for you.尽管如此,您也可以查看https://www.npmjs.com/package/@welldone-software/why-did-you-render package,它可能对您有所帮助。

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

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