Skip to the content.

batch()

batch((fn: Function))

added in v7.0.0

Reactunstable_batchedUpdates() API允许任何在事件循环中的React更新被批处理到一个渲染传递中。React已经在内部将其用于自己的事件处理程序回调。这个API实际上是React domReact Native等渲染器包的一部分,而不是React core本身。

React’s unstable_batchedUpdates() API allows any React updates in an event loop tick to be batched together into a single render pass. React already uses this internally for its own event handler callbacks. This API is actually part of the renderer packages like ReactDOM and React Native, not the React core itself.

因为React-redux需要在ReactDOMReact原生环境中工作,所以我们在构建时从正确的渲染器导入了这个API,供我们自己使用。现在我们自己也公开地重新导出这个函数,并重命名为batch()。你可以使用它来确保在React之外分派的多个动作只会导致一个渲染更新,像这样:

Since React-Redux needs to work in both ReactDOM and React Native environments, we’ve taken care of importing this API from the correct renderer at build time for our own use. We also now re-export this function publicly ourselves, renamed to batch(). You can use it to ensure that multiple actions dispatched outside of React only result in a single render update, like this:

import { batch } from 'react-redux'

function myThunk() {
  return (dispatch, getState) => {
    // should only result in one combined re-render, not two
    batch(() => {
      dispatch(increment())
      dispatch(increment())
    })
  }
}