跳到主要内容

react 空操作更新列队

该文件当前仅在 ReactBaseClasses 中使用。

一、作用

二、空操作更新列队

/**
* This is the abstract API for an update queue.
* 这是用于更新队列的抽象 API。
*/
const ReactNoopUpdateQueue = {
/**
* Checks whether or not this composite component is mounted.
* 检查该复合组件是否已挂载。
* @param {ReactClass} publicInstance The instance we want to test.
* 我们将测试该实例
* @return {boolean} True if mounted, false otherwise.
* 若挂载完成则返回 true ,否则为假
* @protected
* @final
*/
isMounted: function (publicInstance) {
return false;
},

/**
* Forces an update. This should only be invoked when it is known with
* certainty that we are **not** in a DOM transaction.
* 强制更新。只有在确定我们**不**处于 DOM 事务中时才应调用此方法。
*
* You may want to call this when you know that some deeper aspect of the
* component's state has changed but `setState` was not called.
*
* 当你知道组件状态的某些更深层次的方面已经发生变化,但没有调用 `setState` 时,你可能
* 想调用这个。
*
* This will not invoke `shouldComponentUpdate`, but it will invoke
* `componentWillUpdate` and `componentDidUpdate`.
*
* 这不会调用 `shouldComponentUpdate`,但会调用 `componentWillUpdate` 和
* `componentDidUpdate`。
*
* @param {ReactClass} publicInstance The instance that should rerender.
* 该实例应当渲染
* @param {?function} callback Called after component is updated.
* 在组件更新后调用。
* @param {?string} callerName name of the calling function in the public API.
* 公共 API 中调用函数的名称。
* @internal
*/
enqueueForceUpdate: function (publicInstance, callback, callerName) {
warnNoop(publicInstance, 'forceUpdate');
},

/**
* Replaces all of the state. Always use this or `setState` to mutate state.
* You should treat `this.state` as immutable.
* 替换所有的状态。请始终使用此方法或 `setState` 来修改状态。你应该将 `this.state` 视为不可变的。
*
* There is no guarantee that `this.state` will be immediately updated, so
* accessing `this.state` after calling this method may return the old value.
* 无法保证 `this.state` 会立即更新,因此在调用此方法后访问 `this.state` 可能会返回旧值。
*
* @param {ReactClass} publicInstance The instance that should rerender.
* 该实例应当渲染
* @param {object} completeState Next state.
* 下一个状态
* @param {?function} callback Called after component is updated.
* 在组件更新后调用。
* @param {?string} callerName name of the calling function in the public API.
* 公共 API 中调用函数的名称。
* @internal
*/
enqueueReplaceState: function (
publicInstance,
completeState,
callback,
callerName,
) {
warnNoop(publicInstance, 'replaceState');
},

/**
* Sets a subset of the state. This only exists because _pendingState is
* internal. This provides a merging strategy that is not available to deep
* properties which is confusing. TODO: Expose pendingState or don't use it
* during the merge.
*
* 设置状态的一个子集。这个存在的唯一原因是 _pendingState 是内部的。这提供了一种对深层属性
* 不可用的合并策略,这令人困惑。TODO:公开 pendingState 或在合并过程中不要使用它。
*
* @param {ReactClass} publicInstance The instance that should rerender.
* 应该重新渲染的实例。
* @param {object} partialState Next partial state to be merged with state.
* 下一个要合并到状态的部分状态。
* @param {?function} callback Called after component is updated.
* 在组件更新后调用。
* @param {?string} Name of the calling function in the public API.
* 在公共 API 中调用函数的函数。
* @internal
*/
enqueueSetState: function (
publicInstance,
partialState,
callback,
callerName,
) {
warnNoop(publicInstance, 'setState');
},
};

三、常量

// 已警告针对已卸载组件的状态更新
const didWarnStateUpdateForUnmountedComponent = {};

四、工具

// 警告无操作
function warnNoop(publicInstance, callerName) {
if (__DEV__) {
const constructor = publicInstance.constructor;
const componentName =
(constructor && (constructor.displayName || constructor.name)) ||
'ReactClass';
const warningKey = `${componentName}.${callerName}`;
if (didWarnStateUpdateForUnmountedComponent[warningKey]) {
return;
}
console.error(
"Can't call %s on a component that is not yet mounted. " +
'This is a no-op, but it might indicate a bug in your application. ' +
'Instead, assign to `this.state` directly or define a `state = {};` ' +
'class property with the desired state in the %s component.',
callerName,
componentName,
);
didWarnStateUpdateForUnmountedComponent[warningKey] = true;
}
}