Rename inputsAreEqual to areHookInputsEqual & move it to shared (#14036)
Mateusz Burzyński committed
Nov 2, 2018 at 20:50 UTC
ae196e84b656fc75638249b25c4d7b97581ba825
3 files changed
+46
-54
packages/react-dom/src/server/ReactPartialRendererHooks.js
+2
-20
@@ -7,6 +7,7 @@
7
* @flow
8
*/
9
import type {ReactContext} from 'shared/ReactTypes';
10
+import areHookInputsEqual from 'shared/areHookInputsEqual';
11
12
import invariant from 'shared/invariant';
13
import warning from 'shared/warning';
@@ -236,7 +237,7 @@ function useMemo<T>(
237
) {
238
const prevState = workInProgressHook.memoizedState;
239
const prevInputs = prevState[1];
239
- if (inputsAreEqual(nextInputs, prevInputs)) {
240
+ if (areHookInputsEqual(nextInputs, prevInputs)) {
241
return prevState[0];
242
}
243
}
@@ -331,25 +332,6 @@ function dispatchAction<A>(
332
}
333
}
334
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
-
335
function noop(): void {}
336
337
export const Dispatcher = {
packages/react-reconciler/src/ReactFiberHooks.js
+4
-34
@@ -36,7 +36,7 @@ import {
36
} from './ReactFiberScheduler';
37
38
import invariant from 'shared/invariant';
39
-import warning from 'shared/warning';
39
+import areHookInputsEqual from 'shared/areHookInputsEqual';
40
41
type Update<A> = {
42
expirationTime: ExpirationTime,
@@ -545,7 +545,7 @@ function useEffectImpl(fiberEffectTag, hookEffectTag, create, inputs): void {
545
if (currentHook !== null) {
546
const prevEffect = currentHook.memoizedState;
547
destroy = prevEffect.destroy;
548
- if (inputsAreEqual(nextInputs, prevEffect.inputs)) {
548
+ if (areHookInputsEqual(nextInputs, prevEffect.inputs)) {
549
pushEffect(NoHookEffect, create, destroy, nextInputs);
550
return;
551
}
@@ -609,7 +609,7 @@ export function useCallback<T>(
609
const prevState = workInProgressHook.memoizedState;
610
if (prevState !== null) {
611
const prevInputs = prevState[1];
612
- if (inputsAreEqual(nextInputs, prevInputs)) {
612
+ if (areHookInputsEqual(nextInputs, prevInputs)) {
613
return prevState[0];
614
}
615
}
@@ -630,7 +630,7 @@ export function useMemo<T>(
630
const prevState = workInProgressHook.memoizedState;
631
if (prevState !== null) {
632
const prevInputs = prevState[1];
633
- if (inputsAreEqual(nextInputs, prevInputs)) {
633
+ if (areHookInputsEqual(nextInputs, prevInputs)) {
634
return prevState[0];
635
}
636
}
@@ -701,33 +701,3 @@ function dispatchAction<A>(fiber: Fiber, queue: UpdateQueue<A>, action: A) {
701
scheduleWork(fiber, expirationTime);
702
}
703
}
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
-}
packages/shared/areHookInputsEqual.js
new
+40
@@ -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
+}