React库中的context和updater参数是什么?

Ish*_*tel 14 javascript reactjs react-component

我试图了解通过该阵营阵营库,但不明白什么context以及updater是,这是在向组件传递.

以下是库中的代码.

function Component(props, context, updater) {
  this.props = props;
  this.context = context;
  this.refs = emptyObject;
  // We initialize the default updater but the real one gets injected by the
  // renderer.
  this.updater = updater || ReactNoopUpdateQueue;
}
Run Code Online (Sandbox Code Playgroud)

这些东西的目的是什么?

Arm*_*ran 14

上下文

的目的context是使开发者通过props直接components的,而不是通过propschildren/ parents(其可以获得非常复杂/混乱).

在某些情况下,您希望通过组件树传递数据,而无需在每个级别手动传递道具.您可以使用功能强大的"上下文"API直接在React中执行此操作.


更新

updater是一个object包含methods更新DOM.

这是明显的线条6179.

// Line 61: Enqueue setState.
this.updater.enqueueSetState(this, partialState, callback, 'setState') 

// Line 79: Force Update.
this.updater.enqueueForceUpdate(this, callback, 'forceUpdate') 
Run Code Online (Sandbox Code Playgroud)

这些methods是分别使用setState()和触发的forceUpdate().