最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the purpose of the React.addons.batchedUpdates API? - Stack Overflow

programmeradmin2浏览0评论

The React v0.12 release announcement included the following:

New Features:

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

However I cannot find any documentation for this API. What is its purpose?

Specifically, any chance that it has an equivalent of Ember.run()?

The React v0.12 release announcement included the following:

New Features:

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

However I cannot find any documentation for this API. What is its purpose?

Specifically, any chance that it has an equivalent of Ember.run()?

Share Improve this question asked Nov 16, 2014 at 4:27 Josh HabermanJosh Haberman 4,2301 gold badge27 silver badges47 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

When responding to synthetic events like onClick and so on, ponent state changes are batched so lots of calls to this.setState for the same ponent will only result in one render.

If you are changing state in response to some other async callback (e.g. AJAX or setTimeout) then every call to this.setState will result in a render. 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 ...
    })
});

The default batched update strategy is great for your average website. Sometimes you have extra requirements and need to deviate from that.

The initial reason this was made public is for a requestAnimationFrame batching strategy, which is better for games and sites that need to update often and in many places.

It's just an extensibility point to solve edge case issues.

发布评论

评论列表(0)

  1. 暂无评论