Skip to content

Commit ae196e8

Browse files
Andaristsophiebits
authored andcommitted
Rename inputsAreEqual to areHookInputsEqual & move it to shared (#14036)
1 parent c898020 commit ae196e8

File tree

3 files changed

+46
-54
lines changed

3 files changed

+46
-54
lines changed

packages/react-dom/src/server/ReactPartialRendererHooks.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @flow
88
*/
99
import type {ReactContext} from 'shared/ReactTypes';
10+
import areHookInputsEqual from 'shared/areHookInputsEqual';
1011

1112
import invariant from 'shared/invariant';
1213
import warning from 'shared/warning';
@@ -236,7 +237,7 @@ function useMemo<T>(
236237
) {
237238
const prevState = workInProgressHook.memoizedState;
238239
const prevInputs = prevState[1];
239-
if (inputsAreEqual(nextInputs, prevInputs)) {
240+
if (areHookInputsEqual(nextInputs, prevInputs)) {
240241
return prevState[0];
241242
}
242243
}
@@ -331,25 +332,6 @@ function dispatchAction<A>(
331332
}
332333
}
333334

334-
function inputsAreEqual(arr1, arr2) {
335-
// Don't bother comparing lengths because these arrays are always
336-
// passed inline.
337-
for (let i = 0; i < arr1.length; i++) {
338-
// Inlined Object.is polyfill.
339-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
340-
const val1 = arr1[i];
341-
const val2 = arr2[i];
342-
if (
343-
(val1 === val2 && (val1 !== 0 || 1 / val1 === 1 / (val2: any))) ||
344-
(val1 !== val1 && val2 !== val2) // eslint-disable-line no-self-compare
345-
) {
346-
continue;
347-
}
348-
return false;
349-
}
350-
return true;
351-
}
352-
353335
function noop(): void {}
354336

355337
export const Dispatcher = {

packages/react-reconciler/src/ReactFiberHooks.js

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
} from './ReactFiberScheduler';
3737

3838
import invariant from 'shared/invariant';
39-
import warning from 'shared/warning';
39+
import areHookInputsEqual from 'shared/areHookInputsEqual';
4040

4141
type Update<A> = {
4242
expirationTime: ExpirationTime,
@@ -545,7 +545,7 @@ function useEffectImpl(fiberEffectTag, hookEffectTag, create, inputs): void {
545545
if (currentHook !== null) {
546546
const prevEffect = currentHook.memoizedState;
547547
destroy = prevEffect.destroy;
548-
if (inputsAreEqual(nextInputs, prevEffect.inputs)) {
548+
if (areHookInputsEqual(nextInputs, prevEffect.inputs)) {
549549
pushEffect(NoHookEffect, create, destroy, nextInputs);
550550
return;
551551
}
@@ -609,7 +609,7 @@ export function useCallback<T>(
609609
const prevState = workInProgressHook.memoizedState;
610610
if (prevState !== null) {
611611
const prevInputs = prevState[1];
612-
if (inputsAreEqual(nextInputs, prevInputs)) {
612+
if (areHookInputsEqual(nextInputs, prevInputs)) {
613613
return prevState[0];
614614
}
615615
}
@@ -630,7 +630,7 @@ export function useMemo<T>(
630630
const prevState = workInProgressHook.memoizedState;
631631
if (prevState !== null) {
632632
const prevInputs = prevState[1];
633-
if (inputsAreEqual(nextInputs, prevInputs)) {
633+
if (areHookInputsEqual(nextInputs, prevInputs)) {
634634
return prevState[0];
635635
}
636636
}
@@ -701,33 +701,3 @@ function dispatchAction<A>(fiber: Fiber, queue: UpdateQueue<A>, action: A) {
701701
scheduleWork(fiber, expirationTime);
702702
}
703703
}
704-
705-
function inputsAreEqual(arr1, arr2) {
706-
// Don't bother comparing lengths in prod because these arrays should be
707-
// passed inline.
708-
if (__DEV__) {
709-
warning(
710-
arr1.length === arr2.length,
711-
'Detected a variable number of hook dependencies. The length of the ' +
712-
'dependencies array should be constant between renders.\n\n' +
713-
'Previous: %s\n' +
714-
'Incoming: %s',
715-
arr1.join(', '),
716-
arr2.join(', '),
717-
);
718-
}
719-
for (let i = 0; i < arr1.length; i++) {
720-
// Inlined Object.is polyfill.
721-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
722-
const val1 = arr1[i];
723-
const val2 = arr2[i];
724-
if (
725-
(val1 === val2 && (val1 !== 0 || 1 / val1 === 1 / (val2: any))) ||
726-
(val1 !== val1 && val2 !== val2) // eslint-disable-line no-self-compare
727-
) {
728-
continue;
729-
}
730-
return false;
731-
}
732-
return true;
733-
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import warning from 'shared/warning';
11+
12+
export default function areHookInputsEqual(arr1: any[], arr2: any[]) {
13+
// Don't bother comparing lengths in prod because these arrays should be
14+
// passed inline.
15+
if (__DEV__) {
16+
warning(
17+
arr1.length === arr2.length,
18+
'Detected a variable number of hook dependencies. The length of the ' +
19+
'dependencies array should be constant between renders.\n\n' +
20+
'Previous: %s\n' +
21+
'Incoming: %s',
22+
arr1.join(', '),
23+
arr2.join(', '),
24+
);
25+
}
26+
for (let i = 0; i < arr1.length; i++) {
27+
// Inlined Object.is polyfill.
28+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
29+
const val1 = arr1[i];
30+
const val2 = arr2[i];
31+
if (
32+
(val1 === val2 && (val1 !== 0 || 1 / val1 === 1 / (val2: any))) ||
33+
(val1 !== val1 && val2 !== val2) // eslint-disable-line no-self-compare
34+
) {
35+
continue;
36+
}
37+
return false;
38+
}
39+
return true;
40+
}

0 commit comments

Comments
 (0)