1

ReactDOM.render accepts an optional callback, which is executed when component is rendered:

ReactDOM.render(element, container[, callback])

Is there a similar callback in React/ReactDOM that is executed when a component in the tree (of any depth) is updated from within, i.e. using a setState?


  • Simply providing componentDidUpdate on the root component won't do, as the method is not triggered on children update: https://codesandbox.io/s/react-example-yjq0r
  • It is possible to subscribe to DOM tree updates using MutationObserver, but I wonder whether React provides this functionality out of the box.
Pavlo
  • 43,301
  • 14
  • 77
  • 113

1 Answers1

2

This can be done using React.Profiler:

<React.Profiler id="app" onRender={callback}>
  <Root />
</React.Profiler>

https://codesandbox.io/s/react-example-lmvzk

Pavlo
  • 43,301
  • 14
  • 77
  • 113