React: Add "afterBatchUpdate" callback or similar

Created on 6 Jul 2016  路  4Comments  路  Source: facebook/react

Do you want to request a _feature_ or report a _bug_?
This is a feature request.

We've discovered that it would be quite useful for us to be able to have React call some user-defined function after every batch of updates. In our case, we would like to ask our datastore to remove unneeded data after every round of updates (say, after a user clicks a button and after the resulting UI changes are done).

The existing lifecycle methods don't seem to provide us a way to achieve this behavior. Currently, we are releasing data from the datastore when we unmount components, which is suboptimal, as the new component we mount in its place might ask for very similar data. If we could have React execute a callback after each round of updates (when all the UI changes quiesce), we could avoid these unnecessary unload/reloads.

@alexcole @pspeter3

Most helpful comment

This sounds like a very bad idea if some component decides to animate and you end up running unrelated clean up code each and every time.

Can't you just queue your clean-up in componentDidUpdate for the relevant components?

All 4 comments

You can pass a callback as the last argument to setState(), forceUpdate(), or top-level ReactDOM.render(). It will get called when the updates have been flushed. Does this satisfy your use case?

We want a callback that will be called after every round of updates is flushed not just specific ones.

This sounds like a very bad idea if some component decides to animate and you end up running unrelated clean up code each and every time.

Can't you just queue your clean-up in componentDidUpdate for the relevant components?

There's some more recent discussion in https://github.com/facebook/react/issues/10900.

Was this page helpful?
0 / 5 - 0 ratings