Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions packages/react-dom/src/server/ReactPartialRendererHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @flow
*/
import type {ReactContext} from 'shared/ReactTypes';
import areHookInputsEqual from 'shared/areHookInputsEqual';

import invariant from 'shared/invariant';
import warning from 'shared/warning';
Expand Down Expand Up @@ -236,7 +237,7 @@ function useMemo<T>(
) {
const prevState = workInProgressHook.memoizedState;
const prevInputs = prevState[1];
if (inputsAreEqual(nextInputs, prevInputs)) {
if (areHookInputsEqual(nextInputs, prevInputs)) {
return prevState[0];
}
}
Expand Down Expand Up @@ -331,25 +332,6 @@ function dispatchAction<A>(
}
}

function inputsAreEqual(arr1, arr2) {
// Don't bother comparing lengths because these arrays are always
// passed inline.
for (let i = 0; i < arr1.length; i++) {
// Inlined Object.is polyfill.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
const val1 = arr1[i];
const val2 = arr2[i];
if (
(val1 === val2 && (val1 !== 0 || 1 / val1 === 1 / (val2: any))) ||
(val1 !== val1 && val2 !== val2) // eslint-disable-line no-self-compare
) {
continue;
}
return false;
}
return true;
}

function noop(): void {}

export const Dispatcher = {
Expand Down
38 changes: 4 additions & 34 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
} from './ReactFiberScheduler';

import invariant from 'shared/invariant';
import warning from 'shared/warning';
import areHookInputsEqual from 'shared/areHookInputsEqual';

type Update<A> = {
expirationTime: ExpirationTime,
Expand Down Expand Up @@ -548,7 +548,7 @@ function useEffectImpl(fiberEffectTag, hookEffectTag, create, inputs): void {
if (currentHook !== null) {
const prevEffect = currentHook.memoizedState;
destroy = prevEffect.destroy;
if (inputsAreEqual(nextInputs, prevEffect.inputs)) {
if (areHookInputsEqual(nextInputs, prevEffect.inputs)) {
pushEffect(NoHookEffect, create, destroy, nextInputs);
return;
}
Expand Down Expand Up @@ -612,7 +612,7 @@ export function useCallback<T>(
const prevState = workInProgressHook.memoizedState;
if (prevState !== null) {
const prevInputs = prevState[1];
if (inputsAreEqual(nextInputs, prevInputs)) {
if (areHookInputsEqual(nextInputs, prevInputs)) {
return prevState[0];
}
}
Expand All @@ -633,7 +633,7 @@ export function useMemo<T>(
const prevState = workInProgressHook.memoizedState;
if (prevState !== null) {
const prevInputs = prevState[1];
if (inputsAreEqual(nextInputs, prevInputs)) {
if (areHookInputsEqual(nextInputs, prevInputs)) {
return prevState[0];
}
}
Expand Down Expand Up @@ -704,33 +704,3 @@ function dispatchAction<A>(fiber: Fiber, queue: UpdateQueue<A>, action: A) {
scheduleWork(fiber, expirationTime);
}
}

function inputsAreEqual(arr1, arr2) {
// Don't bother comparing lengths in prod because these arrays should be
// passed inline.
if (__DEV__) {
warning(
arr1.length === arr2.length,
'Detected a variable number of hook dependencies. The length of the ' +
'dependencies array should be constant between renders.\n\n' +
'Previous: %s\n' +
'Incoming: %s',
arr1.join(', '),
arr2.join(', '),
);
}
for (let i = 0; i < arr1.length; i++) {
// Inlined Object.is polyfill.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
const val1 = arr1[i];
const val2 = arr2[i];
if (
(val1 === val2 && (val1 !== 0 || 1 / val1 === 1 / (val2: any))) ||
(val1 !== val1 && val2 !== val2) // eslint-disable-line no-self-compare
) {
continue;
}
return false;
}
return true;
}
40 changes: 40 additions & 0 deletions packages/shared/areHookInputsEqual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import warning from 'shared/warning';

export default function areHookInputsEqual(arr1: any[], arr2: any[]) {
// Don't bother comparing lengths in prod because these arrays should be
// passed inline.
if (__DEV__) {
warning(
arr1.length === arr2.length,
'Detected a variable number of hook dependencies. The length of the ' +
'dependencies array should be constant between renders.\n\n' +
'Previous: %s\n' +
'Incoming: %s',
arr1.join(', '),
arr2.join(', '),
);
}
for (let i = 0; i < arr1.length; i++) {
// Inlined Object.is polyfill.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
const val1 = arr1[i];
const val2 = arr2[i];
if (
(val1 === val2 && (val1 !== 0 || 1 / val1 === 1 / (val2: any))) ||
(val1 !== val1 && val2 !== val2) // eslint-disable-line no-self-compare
) {
continue;
}
return false;
}
return true;
}