Props updated, componentDidUpdate did not fire

2019-07-20 13:21发布

Is there ever a situation where componentDidUpdate would NOT fire, even if the props updated in React?

3条回答
贼婆χ
2楼-- · 2019-07-20 13:38

this can also happen, if your component is in a list and the key of your component changes (ie. on every render) or the key is missing, or the key is duplicate, etc.

refer to docs for details: https://reactjs.org/docs/lists-and-keys.html

查看更多
看我几分像从前
3楼-- · 2019-07-20 13:44

There are 3 conditions that can cause componentDidUpdate to not fire:

1) You wrote a custom shouldComponentUpdate that returned false.

2) The internal react methods determined that there was no change in the virtual DOM, so it chose not to re-render.

3) You extended something other than React.Component (such as PureComponent) which has a default shouldComponentUpdate method that returned false.

查看更多
在下西门庆
4楼-- · 2019-07-20 13:57

You can suppress a render with returning false in shouldComponentUpdate(). So yes, in that case componentDidUpdate() won't fire.

查看更多
登录 后发表回答