What is the purpose of the React.addons.batchedUpdates API?

The React v0.12 release announcement included the following:

New opportunities:

* React.addons.batchedUpdates added to API for hooking into update cycle 

However, I can not find the documentation for this API. What is his purpose?

In particular, is there any chance that it has the equivalent of Ember.run() ?

+5
source share
2 answers

Responding to synthetic events, such as onClick, etc., changes the state of the component, so many calls to this.setState for the same component will lead to only one rendering.

If you change state in response to some other asynchronous callback (for example, AJAX or setTimeout), then every call to this .setState will result in rendering. You can wrap your work in batchedUpdates (..) to avoid this.

 var React = require('react/addons'); var batchedUpdates = React.addons.batchedUpdates; var request = require('superagent'); // AJAX lib var req = request('GET', ...).end(function(err, res) { // invoked when AJAX call is done batchedUpdates(function(){ .. all setState calls are batched and only one render is done ... }) }); 
+7
source

The default batch update strategy is great for your average website. Sometimes you have additional requirements and you need to deviate from this.

The original reason this was made public is because of the requestAnimationFrame batch processing strategy, which is better for games and sites that need to be updated frequently in many places.

It is simply an extensibility point to solve problems with edge cases.

0
source

Source: https://habr.com/ru/post/1206966/


All Articles