Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce forceSyncUpdate to class component #28957

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 34 additions & 1 deletion packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import {
cloneUpdateQueue,
suspendIfUpdateReadFromEntangledAsyncAction,
} from './ReactFiberClassUpdateQueue';
import {NoLanes} from './ReactFiberLane';
import {NoLanes, SyncLane} from './ReactFiberLane';
import {
cacheContext,
getMaskedContext,
Expand Down Expand Up @@ -295,6 +295,39 @@ const classComponentUpdater = {
markForceUpdateScheduled(fiber, lane);
}
},
// $FlowFixMe[missing-local-annot]
enqueueForceSyncUpdate(inst: any, callback) {
const fiber = getInstance(inst);

const update = createUpdate(SyncLane);
update.tag = ForceUpdate;

if (callback !== undefined && callback !== null) {
if (__DEV__) {
warnOnInvalidCallback(callback);
}
update.callback = callback;
}

const root = enqueueUpdate(fiber, update, SyncLane);
if (root !== null) {
scheduleUpdateOnFiber(root, fiber, SyncLane);
entangleTransitions(root, fiber, SyncLane);
}

if (__DEV__) {
if (enableDebugTracing) {
if (fiber.mode & DebugTracingMode) {
const name = getComponentNameFromFiber(fiber) || 'Unknown';
logForceUpdateScheduled(name, SyncLane);
}
}
}

if (enableSchedulingProfiler) {
markForceUpdateScheduled(fiber, SyncLane);
}
},
};

function checkShouldComponentUpdate(
Expand Down
13 changes: 13 additions & 0 deletions packages/react-server/src/ReactFizzClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ const classComponentUpdater = {
}
}
},
// $FlowFixMe[missing-local-annot]
enqueueForceSyncUpdate(inst: any, callback) {
const internals: InternalInstance = getInstance(inst);
if (internals.queue === null) {
warnNoop(inst, 'forceSyncUpdate');
} else {
if (__DEV__) {
if (callback !== undefined && callback !== null) {
warnOnInvalidCallback(callback);
}
}
}
},
};

function applyDerivedStateFromProps(
Expand Down
19 changes: 19 additions & 0 deletions packages/react/src/ReactBaseClasses.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ Component.prototype.forceUpdate = function (callback) {
this.updater.enqueueForceUpdate(this, callback, 'forceUpdate');
};

/**
* Forces an update in SyncLane. This should only be invoked when it is known
* with certainty that we are **not** in a DOM transaction and need the changes
* to be scheduled immediately.
*
* 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.
*
* This will not invoke `shouldComponentUpdate`, but it will invoke
* `componentWillUpdate` and `componentDidUpdate`.
*
* @param {?function} callback Called after update is complete.
* @final
* @protected
*/
Component.prototype.forceSyncUpdate = function (callback) {
this.updater.enqueueForceSyncUpdate(this, callback, 'forceSyncUpdate');
};

/**
* Deprecated APIs. These APIs used to exist on classic React classes but since
* we would like to deprecate them, we're not going to move them over to this
Expand Down
20 changes: 20 additions & 0 deletions packages/react/src/ReactNoopUpdateQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ const ReactNoopUpdateQueue = {
warnNoop(publicInstance, 'forceUpdate');
},

/**
* Forces an update in SyncLane. This should only be invoked when it is known
* with certainty that we are **not** in a DOM transaction and need the changes
* to be scheduled immediately.
*
* 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.
*
* This will not invoke `shouldComponentUpdate`, but it will invoke
* `componentWillUpdate` and `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.
* @internal
*/
enqueueForceSyncUpdate: function (publicInstance, callback, callerName) {
warnNoop(publicInstance, 'forceSyncUpdate');
},

/**
* Replaces all of the state. Always use this or `setState` to mutate state.
* You should treat `this.state` as immutable.
Expand Down