Skip to content

Commit

Permalink
Fix: useOptimistic should return passthrough value when there are no …
Browse files Browse the repository at this point in the history
…updates pending (#27936)

This fixes a bug that happened when the canonical value passed to
useOptimistic without an accompanying call to setOptimistic. In this
scenario, useOptimistic should pass through the new canonical value.

I had written tests for the more complicated scenario, where a new value
is passed while there are still pending optimistic updates, but not this
simpler one.

DiffTrain build for [60a927d](60a927d)
  • Loading branch information
acdlite committed Jan 14, 2024
1 parent 990d5cc commit f10aaf1
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 65 deletions.
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0ac3ea471fbcb7d79bc7d36179e960c72c779e76
60a927d04ad3888facebcdf7da620aa1cfc9528f
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if (__DEV__) {
) {
__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
}
var ReactVersion = "18.3.0-www-classic-23db00d6";
var ReactVersion = "18.3.0-www-classic-6f4b3c51";

// ATTENTION
// When adding new symbols to this file,
Expand Down
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-prod.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,4 @@ exports.useSyncExternalStore = function (
exports.useTransition = function () {
return ReactCurrentDispatcher.current.useTransition();
};
exports.version = "18.3.0-www-classic-db2e638d";
exports.version = "18.3.0-www-classic-7b414a88";
15 changes: 12 additions & 3 deletions compiled/facebook-www/ReactART-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ if (__DEV__) {
return self;
}

var ReactVersion = "18.3.0-www-classic-47994933";
var ReactVersion = "18.3.0-www-classic-87e74e5e";

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down Expand Up @@ -9051,10 +9051,19 @@ if (__DEV__) {
queue.pending = null;
}

if (baseQueue !== null) {
var baseState = hook.baseState;

if (baseQueue === null) {
// If there are no pending updates, then the memoized state should be the
// same as the base state. Currently these only diverge in the case of
// useOptimistic, because useOptimistic accepts a new baseState on
// every render.
hook.memoizedState = baseState; // We don't need to call markWorkInProgressReceivedUpdate because
// baseState is derived from other reactive values.
} else {
// We have a queue to process.
var first = baseQueue.next;
var newState = hook.baseState;
var newState = baseState;
var newBaseState = null;
var newBaseQueueFirst = null;
var newBaseQueueLast = null;
Expand Down
15 changes: 12 additions & 3 deletions compiled/facebook-www/ReactART-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ if (__DEV__) {
return self;
}

var ReactVersion = "18.3.0-www-modern-e805922c";
var ReactVersion = "18.3.0-www-modern-dc8082dd";

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down Expand Up @@ -8801,10 +8801,19 @@ if (__DEV__) {
queue.pending = null;
}

if (baseQueue !== null) {
var baseState = hook.baseState;

if (baseQueue === null) {
// If there are no pending updates, then the memoized state should be the
// same as the base state. Currently these only diverge in the case of
// useOptimistic, because useOptimistic accepts a new baseState on
// every render.
hook.memoizedState = baseState; // We don't need to call markWorkInProgressReceivedUpdate because
// baseState is derived from other reactive values.
} else {
// We have a queue to process.
var first = baseQueue.next;
var newState = hook.baseState;
var newState = baseState;
var newBaseState = null;
var newBaseQueueFirst = null;
var newBaseQueueLast = null;
Expand Down
9 changes: 5 additions & 4 deletions compiled/facebook-www/ReactART-prod.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2860,9 +2860,10 @@ function updateReducerImpl(hook, current, reducer) {
current.baseQueue = baseQueue = pendingQueue;
queue.pending = null;
}
if (null !== baseQueue) {
pendingQueue = hook.baseState;
if (null === baseQueue) hook.memoizedState = pendingQueue;
else {
current = baseQueue.next;
pendingQueue = hook.baseState;
var newBaseQueueFirst = (baseFirst = null),
newBaseQueueLast = null,
update = current;
Expand Down Expand Up @@ -10291,7 +10292,7 @@ var slice = Array.prototype.slice,
return null;
},
bundleType: 0,
version: "18.3.0-www-classic-d67c518f",
version: "18.3.0-www-classic-c9d1dfab",
rendererPackageName: "react-art"
};
var internals$jscomp$inline_1325 = {
Expand Down Expand Up @@ -10322,7 +10323,7 @@ var internals$jscomp$inline_1325 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-www-classic-d67c518f"
reconcilerVersion: "18.3.0-www-classic-c9d1dfab"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1326 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
9 changes: 5 additions & 4 deletions compiled/facebook-www/ReactART-prod.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -2667,9 +2667,10 @@ function updateReducerImpl(hook, current, reducer) {
current.baseQueue = baseQueue = pendingQueue;
queue.pending = null;
}
if (null !== baseQueue) {
pendingQueue = hook.baseState;
if (null === baseQueue) hook.memoizedState = pendingQueue;
else {
current = baseQueue.next;
pendingQueue = hook.baseState;
var newBaseQueueFirst = (baseFirst = null),
newBaseQueueLast = null,
update = current;
Expand Down Expand Up @@ -9957,7 +9958,7 @@ var slice = Array.prototype.slice,
return null;
},
bundleType: 0,
version: "18.3.0-www-modern-bab56d31",
version: "18.3.0-www-modern-31c1a5e7",
rendererPackageName: "react-art"
};
var internals$jscomp$inline_1305 = {
Expand Down Expand Up @@ -9988,7 +9989,7 @@ var internals$jscomp$inline_1305 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-www-modern-bab56d31"
reconcilerVersion: "18.3.0-www-modern-31c1a5e7"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1306 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
15 changes: 12 additions & 3 deletions compiled/facebook-www/ReactDOM-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -13662,10 +13662,19 @@ if (__DEV__) {
queue.pending = null;
}

if (baseQueue !== null) {
var baseState = hook.baseState;

if (baseQueue === null) {
// If there are no pending updates, then the memoized state should be the
// same as the base state. Currently these only diverge in the case of
// useOptimistic, because useOptimistic accepts a new baseState on
// every render.
hook.memoizedState = baseState; // We don't need to call markWorkInProgressReceivedUpdate because
// baseState is derived from other reactive values.
} else {
// We have a queue to process.
var first = baseQueue.next;
var newState = hook.baseState;
var newState = baseState;
var newBaseState = null;
var newBaseQueueFirst = null;
var newBaseQueueLast = null;
Expand Down Expand Up @@ -34959,7 +34968,7 @@ if (__DEV__) {
return root;
}

var ReactVersion = "18.3.0-www-classic-df230f01";
var ReactVersion = "18.3.0-www-classic-1829de97";

function createPortal$1(
children,
Expand Down
15 changes: 12 additions & 3 deletions compiled/facebook-www/ReactDOM-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -13598,10 +13598,19 @@ if (__DEV__) {
queue.pending = null;
}

if (baseQueue !== null) {
var baseState = hook.baseState;

if (baseQueue === null) {
// If there are no pending updates, then the memoized state should be the
// same as the base state. Currently these only diverge in the case of
// useOptimistic, because useOptimistic accepts a new baseState on
// every render.
hook.memoizedState = baseState; // We don't need to call markWorkInProgressReceivedUpdate because
// baseState is derived from other reactive values.
} else {
// We have a queue to process.
var first = baseQueue.next;
var newState = hook.baseState;
var newState = baseState;
var newBaseState = null;
var newBaseQueueFirst = null;
var newBaseQueueLast = null;
Expand Down Expand Up @@ -34780,7 +34789,7 @@ if (__DEV__) {
return root;
}

var ReactVersion = "18.3.0-www-modern-332dd66b";
var ReactVersion = "18.3.0-www-modern-bac1e3ec";

function createPortal$1(
children,
Expand Down
11 changes: 6 additions & 5 deletions compiled/facebook-www/ReactDOM-prod.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3600,9 +3600,10 @@ function updateReducerImpl(hook, current, reducer) {
current.baseQueue = baseQueue = pendingQueue;
queue.pending = null;
}
if (null !== baseQueue) {
pendingQueue = hook.baseState;
if (null === baseQueue) hook.memoizedState = pendingQueue;
else {
current = baseQueue.next;
pendingQueue = hook.baseState;
var newBaseQueueFirst = (baseFirst = null),
newBaseQueueLast = null,
update = current;
Expand Down Expand Up @@ -16595,7 +16596,7 @@ Internals.Events = [
var devToolsConfig$jscomp$inline_1799 = {
findFiberByHostInstance: getClosestInstanceFromNode,
bundleType: 0,
version: "18.3.0-www-classic-f0f463da",
version: "18.3.0-www-classic-be8e039e",
rendererPackageName: "react-dom"
};
var internals$jscomp$inline_2145 = {
Expand Down Expand Up @@ -16625,7 +16626,7 @@ var internals$jscomp$inline_2145 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-www-classic-f0f463da"
reconcilerVersion: "18.3.0-www-classic-be8e039e"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_2146 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down Expand Up @@ -16962,4 +16963,4 @@ exports.useFormState = function () {
exports.useFormStatus = function () {
throw Error(formatProdErrorMessage(248));
};
exports.version = "18.3.0-www-classic-f0f463da";
exports.version = "18.3.0-www-classic-be8e039e";
11 changes: 6 additions & 5 deletions compiled/facebook-www/ReactDOM-prod.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -3494,9 +3494,10 @@ function updateReducerImpl(hook, current, reducer) {
current.baseQueue = baseQueue = pendingQueue;
queue.pending = null;
}
if (null !== baseQueue) {
pendingQueue = hook.baseState;
if (null === baseQueue) hook.memoizedState = pendingQueue;
else {
current = baseQueue.next;
pendingQueue = hook.baseState;
var newBaseQueueFirst = (baseFirst = null),
newBaseQueueLast = null,
update = current;
Expand Down Expand Up @@ -16118,7 +16119,7 @@ Internals.Events = [
var devToolsConfig$jscomp$inline_1758 = {
findFiberByHostInstance: getClosestInstanceFromNode,
bundleType: 0,
version: "18.3.0-www-modern-b1186d41",
version: "18.3.0-www-modern-75306c51",
rendererPackageName: "react-dom"
};
var internals$jscomp$inline_2109 = {
Expand Down Expand Up @@ -16149,7 +16150,7 @@ var internals$jscomp$inline_2109 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-www-modern-b1186d41"
reconcilerVersion: "18.3.0-www-modern-75306c51"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_2110 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down Expand Up @@ -16414,4 +16415,4 @@ exports.useFormState = function () {
exports.useFormStatus = function () {
throw Error(formatProdErrorMessage(248));
};
exports.version = "18.3.0-www-modern-b1186d41";
exports.version = "18.3.0-www-modern-75306c51";
11 changes: 6 additions & 5 deletions compiled/facebook-www/ReactDOM-profiling.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3740,9 +3740,10 @@ function updateReducerImpl(hook, current, reducer) {
current.baseQueue = baseQueue = pendingQueue;
queue.pending = null;
}
if (null !== baseQueue) {
pendingQueue = hook.baseState;
if (null === baseQueue) hook.memoizedState = pendingQueue;
else {
current = baseQueue.next;
pendingQueue = hook.baseState;
var newBaseQueueFirst = (baseFirst = null),
newBaseQueueLast = null,
update = current;
Expand Down Expand Up @@ -17364,7 +17365,7 @@ Internals.Events = [
var devToolsConfig$jscomp$inline_1884 = {
findFiberByHostInstance: getClosestInstanceFromNode,
bundleType: 0,
version: "18.3.0-www-classic-f1e06a21",
version: "18.3.0-www-classic-3a859445",
rendererPackageName: "react-dom"
};
(function (internals) {
Expand Down Expand Up @@ -17408,7 +17409,7 @@ var devToolsConfig$jscomp$inline_1884 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-www-classic-f1e06a21"
reconcilerVersion: "18.3.0-www-classic-3a859445"
});
assign(Internals, {
ReactBrowserEventEmitter: {
Expand Down Expand Up @@ -17732,7 +17733,7 @@ exports.useFormState = function () {
exports.useFormStatus = function () {
throw Error(formatProdErrorMessage(248));
};
exports.version = "18.3.0-www-classic-f1e06a21";
exports.version = "18.3.0-www-classic-3a859445";
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
"function" ===
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&
Expand Down
11 changes: 6 additions & 5 deletions compiled/facebook-www/ReactDOM-profiling.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -3634,9 +3634,10 @@ function updateReducerImpl(hook, current, reducer) {
current.baseQueue = baseQueue = pendingQueue;
queue.pending = null;
}
if (null !== baseQueue) {
pendingQueue = hook.baseState;
if (null === baseQueue) hook.memoizedState = pendingQueue;
else {
current = baseQueue.next;
pendingQueue = hook.baseState;
var newBaseQueueFirst = (baseFirst = null),
newBaseQueueLast = null,
update = current;
Expand Down Expand Up @@ -16881,7 +16882,7 @@ Internals.Events = [
var devToolsConfig$jscomp$inline_1843 = {
findFiberByHostInstance: getClosestInstanceFromNode,
bundleType: 0,
version: "18.3.0-www-modern-e805922c",
version: "18.3.0-www-modern-dc8082dd",
rendererPackageName: "react-dom"
};
(function (internals) {
Expand Down Expand Up @@ -16926,7 +16927,7 @@ var devToolsConfig$jscomp$inline_1843 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-www-modern-e805922c"
reconcilerVersion: "18.3.0-www-modern-dc8082dd"
});
exports.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = Internals;
exports.createPortal = function (children, container) {
Expand Down Expand Up @@ -17178,7 +17179,7 @@ exports.useFormState = function () {
exports.useFormStatus = function () {
throw Error(formatProdErrorMessage(248));
};
exports.version = "18.3.0-www-modern-e805922c";
exports.version = "18.3.0-www-modern-dc8082dd";
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
"function" ===
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&
Expand Down
15 changes: 12 additions & 3 deletions compiled/facebook-www/ReactDOMTesting-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -13799,10 +13799,19 @@ if (__DEV__) {
queue.pending = null;
}

if (baseQueue !== null) {
var baseState = hook.baseState;

if (baseQueue === null) {
// If there are no pending updates, then the memoized state should be the
// same as the base state. Currently these only diverge in the case of
// useOptimistic, because useOptimistic accepts a new baseState on
// every render.
hook.memoizedState = baseState; // We don't need to call markWorkInProgressReceivedUpdate because
// baseState is derived from other reactive values.
} else {
// We have a queue to process.
var first = baseQueue.next;
var newState = hook.baseState;
var newState = baseState;
var newBaseState = null;
var newBaseQueueFirst = null;
var newBaseQueueLast = null;
Expand Down Expand Up @@ -35583,7 +35592,7 @@ if (__DEV__) {
return root;
}

var ReactVersion = "18.3.0-www-classic-fb05f953";
var ReactVersion = "18.3.0-www-classic-34ed23b6";

function createPortal$1(
children,
Expand Down
Loading

0 comments on commit f10aaf1

Please sign in to comment.