Skip to content

Commit 0cc4b5e

Browse files
committed
code cleanup
1 parent d084e06 commit 0cc4b5e

File tree

4 files changed

+22
-32
lines changed

4 files changed

+22
-32
lines changed

packages/react-reconciler/src/ReactFiber.new.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,7 @@ export function createWorkInProgress(current: Fiber, pendingProps: any): Fiber {
311311
workInProgress.memoizedProps = current.memoizedProps;
312312
workInProgress.memoizedState = current.memoizedState;
313313
workInProgress.updateQueue = current.updateQueue;
314-
315-
const memoCache = current.memoCache;
316-
if (memoCache !== null) {
317-
workInProgress.memoCache = {
318-
data: memoCache.data.map(data => data.slice()),
319-
index: 0,
320-
};
321-
} else {
322-
workInProgress.memoCache = null;
323-
}
314+
workInProgress.memoCache = current.memoCache;
324315

325316
// Clone the dependencies object. This is mutated during the render phase, so
326317
// it cannot be shared with the current fiber.

packages/react-reconciler/src/ReactFiber.old.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,7 @@ export function createWorkInProgress(current: Fiber, pendingProps: any): Fiber {
311311
workInProgress.memoizedProps = current.memoizedProps;
312312
workInProgress.memoizedState = current.memoizedState;
313313
workInProgress.updateQueue = current.updateQueue;
314-
315-
const memoCache = current.memoCache;
316-
if (memoCache !== null) {
317-
workInProgress.memoCache = {
318-
data: memoCache.data.map(data => data.slice()),
319-
index: 0,
320-
};
321-
} else {
322-
workInProgress.memoCache = null;
323-
}
314+
workInProgress.memoCache = current.memoCache;
324315

325316
// Clone the dependencies object. This is mutated during the render phase, so
326317
// it cannot be shared with the current fiber.

packages/react-reconciler/src/ReactFiberHooks.new.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,14 @@ export function renderWithHooks<Props, SecondArg>(
395395
current !== null && current.type !== workInProgress.type;
396396
}
397397

398+
// Reset the memoCache index and create a backup copy in case of a setState during render
399+
// or error, either of which can leave the cache in an inconsistent state
398400
const memoCache = workInProgress.memoCache;
399401
let previousMemoCache = null;
400402
if (memoCache !== null) {
401-
// Clone the cache to allow resetting to the most recent version in case of a setstate during render
402-
previousMemoCache = memoCache.data.map(data => data.slice());
403+
previousMemoCache = memoCache.data.map(array => array.slice());
403404
if (workInProgress.alternate != null) {
405+
// Backup to the alternate fiber in case the component throws
404406
workInProgress.alternate.memoCache = {
405407
data: previousMemoCache,
406408
index: 0,
@@ -481,9 +483,8 @@ export function renderWithHooks<Props, SecondArg>(
481483
workInProgress.updateQueue = null;
482484

483485
if (memoCache !== null) {
484-
// Setting state during render could leave the cache in a partially filled,
485-
// and therefore inconsistent, state. Reset to the previous value to ensure
486-
// the cache is consistent.
486+
// Setting state during render could leave the cache in an inconsistent state,
487+
// reset to the previous state before re-rendering.
487488
if (previousMemoCache !== null) {
488489
memoCache.data = previousMemoCache.map(data => data.slice());
489490
} else {
@@ -640,10 +641,13 @@ export function resetHooksAfterThrow(erroredWork: Fiber | null): void {
640641
if (erroredWork != null) {
641642
const memoCache = erroredWork.memoCache;
642643
if (memoCache !== null) {
644+
// Unless this is the first render of a component, the alternate will have a
645+
// consistent view of the memo cache that we can restore to
643646
const alternateMemoCache = erroredWork.alternate?.memoCache ?? null;
644647
if (alternateMemoCache !== null) {
645-
memoCache.data = alternateMemoCache.data.map(data => data.slice());
648+
memoCache.data = alternateMemoCache.data.map(array => array.slice());
646649
} else {
650+
// Just in case, fall back to clearing the memo cache
647651
memoCache.data.length = 0;
648652
}
649653
memoCache.index = 0;

packages/react-reconciler/src/ReactFiberHooks.old.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,14 @@ export function renderWithHooks<Props, SecondArg>(
395395
current !== null && current.type !== workInProgress.type;
396396
}
397397

398+
// Reset the memoCache index and create a backup copy in case of a setState during render
399+
// or error, either of which can leave the cache in an inconsistent state
398400
const memoCache = workInProgress.memoCache;
399401
let previousMemoCache = null;
400402
if (memoCache !== null) {
401-
// Clone the cache to allow resetting to the most recent version in case of a setstate during render
402-
previousMemoCache = memoCache.data.map(data => data.slice());
403+
previousMemoCache = memoCache.data.map(array => array.slice());
403404
if (workInProgress.alternate != null) {
405+
// Backup to the alternate fiber in case the component throws
404406
workInProgress.alternate.memoCache = {
405407
data: previousMemoCache,
406408
index: 0,
@@ -481,9 +483,8 @@ export function renderWithHooks<Props, SecondArg>(
481483
workInProgress.updateQueue = null;
482484

483485
if (memoCache !== null) {
484-
// Setting state during render could leave the cache in a partially filled,
485-
// and therefore inconsistent, state. Reset to the previous value to ensure
486-
// the cache is consistent.
486+
// Setting state during render could leave the cache in an inconsistent state,
487+
// reset to the previous state before re-rendering.
487488
if (previousMemoCache !== null) {
488489
memoCache.data = previousMemoCache.map(data => data.slice());
489490
} else {
@@ -640,10 +641,13 @@ export function resetHooksAfterThrow(erroredWork: Fiber | null): void {
640641
if (erroredWork != null) {
641642
const memoCache = erroredWork.memoCache;
642643
if (memoCache !== null) {
644+
// Unless this is the first render of a component, the alternate will have a
645+
// consistent view of the memo cache that we can restore to
643646
const alternateMemoCache = erroredWork.alternate?.memoCache ?? null;
644647
if (alternateMemoCache !== null) {
645-
memoCache.data = alternateMemoCache.data.map(data => data.slice());
648+
memoCache.data = alternateMemoCache.data.map(array => array.slice());
646649
} else {
650+
// Just in case, fall back to clearing the memo cache
647651
memoCache.data.length = 0;
648652
}
649653
memoCache.index = 0;

0 commit comments

Comments
 (0)