Skip to content

Commit 34c3c35

Browse files
committed
Extract act environment check into function
`act` checks the environment to determine whether to fire a warning. We're changing how this check works in React 18. As a first step, this refactors the logic into a single function. No behavior changes yet.
1 parent a45533c commit 34c3c35

File tree

6 files changed

+58
-50
lines changed

6 files changed

+58
-50
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 type {Fiber} from './ReactFiber.new';
11+
import {warnsIfNotActing} from './ReactFiberHostConfig';
12+
13+
import ReactSharedInternals from 'shared/ReactSharedInternals';
14+
15+
const {ReactCurrentActQueue} = ReactSharedInternals;
16+
17+
export function isActEnvironment(fiber: Fiber) {
18+
const disableActWarning = ReactCurrentActQueue.disableActWarning;
19+
// $FlowExpectedError - Flow doesn't know about jest
20+
const jestIsDefined = typeof jest !== 'undefined';
21+
return warnsIfNotActing && jestIsDefined && !disableActWarning;
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 type {Fiber} from './ReactFiber.old';
11+
import {warnsIfNotActing} from './ReactFiberHostConfig';
12+
13+
import ReactSharedInternals from 'shared/ReactSharedInternals';
14+
15+
const {ReactCurrentActQueue} = ReactSharedInternals;
16+
17+
export function isActEnvironment(fiber: Fiber) {
18+
const disableActWarning = ReactCurrentActQueue.disableActWarning;
19+
// $FlowExpectedError - Flow doesn't know about jest
20+
const jestIsDefined = typeof jest !== 'undefined';
21+
return warnsIfNotActing && jestIsDefined && !disableActWarning;
22+
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ import {
118118
} from './ReactUpdateQueue.new';
119119
import {pushInterleavedQueue} from './ReactFiberInterleavedUpdates.new';
120120
import {warnOnSubscriptionInsideStartTransition} from 'shared/ReactFeatureFlags';
121+
import {isActEnvironment} from './ReactFiberAct.new';
121122

122123
const {ReactCurrentDispatcher, ReactCurrentBatchConfig} = ReactSharedInternals;
123124

@@ -1678,8 +1679,7 @@ function mountEffect(
16781679
deps: Array<mixed> | void | null,
16791680
): void {
16801681
if (__DEV__) {
1681-
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
1682-
if ('undefined' !== typeof jest) {
1682+
if (isActEnvironment(currentlyRenderingFiber)) {
16831683
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
16841684
}
16851685
}
@@ -1709,8 +1709,7 @@ function updateEffect(
17091709
deps: Array<mixed> | void | null,
17101710
): void {
17111711
if (__DEV__) {
1712-
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
1713-
if ('undefined' !== typeof jest) {
1712+
if (isActEnvironment(currentlyRenderingFiber)) {
17141713
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
17151714
}
17161715
}
@@ -2193,8 +2192,7 @@ function dispatchReducerAction<S, A>(
21932192
enqueueUpdate(fiber, queue, update, lane);
21942193

21952194
if (__DEV__) {
2196-
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
2197-
if ('undefined' !== typeof jest) {
2195+
if (isActEnvironment(fiber)) {
21982196
warnIfNotCurrentlyActingUpdatesInDev(fiber);
21992197
}
22002198
}
@@ -2279,8 +2277,7 @@ function dispatchSetState<S, A>(
22792277
}
22802278
}
22812279
if (__DEV__) {
2282-
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
2283-
if ('undefined' !== typeof jest) {
2280+
if (isActEnvironment(fiber)) {
22842281
warnIfNotCurrentlyActingUpdatesInDev(fiber);
22852282
}
22862283
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ import {
118118
} from './ReactUpdateQueue.old';
119119
import {pushInterleavedQueue} from './ReactFiberInterleavedUpdates.old';
120120
import {warnOnSubscriptionInsideStartTransition} from 'shared/ReactFeatureFlags';
121+
import {isActEnvironment} from './ReactFiberAct.old';
121122

122123
const {ReactCurrentDispatcher, ReactCurrentBatchConfig} = ReactSharedInternals;
123124

@@ -1678,8 +1679,7 @@ function mountEffect(
16781679
deps: Array<mixed> | void | null,
16791680
): void {
16801681
if (__DEV__) {
1681-
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
1682-
if ('undefined' !== typeof jest) {
1682+
if (isActEnvironment(currentlyRenderingFiber)) {
16831683
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
16841684
}
16851685
}
@@ -1709,8 +1709,7 @@ function updateEffect(
17091709
deps: Array<mixed> | void | null,
17101710
): void {
17111711
if (__DEV__) {
1712-
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
1713-
if ('undefined' !== typeof jest) {
1712+
if (isActEnvironment(currentlyRenderingFiber)) {
17141713
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
17151714
}
17161715
}
@@ -2193,8 +2192,7 @@ function dispatchReducerAction<S, A>(
21932192
enqueueUpdate(fiber, queue, update, lane);
21942193

21952194
if (__DEV__) {
2196-
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
2197-
if ('undefined' !== typeof jest) {
2195+
if (isActEnvironment(fiber)) {
21982196
warnIfNotCurrentlyActingUpdatesInDev(fiber);
21992197
}
22002198
}
@@ -2279,8 +2277,7 @@ function dispatchSetState<S, A>(
22792277
}
22802278
}
22812279
if (__DEV__) {
2282-
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
2283-
if ('undefined' !== typeof jest) {
2280+
if (isActEnvironment(fiber)) {
22842281
warnIfNotCurrentlyActingUpdatesInDev(fiber);
22852282
}
22862283
}

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ import {
8484
scheduleTimeout,
8585
cancelTimeout,
8686
noTimeout,
87-
warnsIfNotActing,
8887
afterActiveInstanceBlur,
8988
clearContainer,
9089
getCurrentEventPriority,
@@ -2816,15 +2815,8 @@ function shouldForceFlushFallbacksInDEV() {
28162815
export function warnIfNotCurrentlyActingEffectsInDEV(fiber: Fiber): void {
28172816
if (__DEV__) {
28182817
if (
2819-
warnsIfNotActing === true &&
28202818
(fiber.mode & StrictLegacyMode) !== NoMode &&
2821-
ReactCurrentActQueue.current === null &&
2822-
// Our internal tests use a custom implementation of `act` that works by
2823-
// mocking the Scheduler package. Disable the `act` warning.
2824-
// TODO: Maybe the warning should be disabled by default, and then turned
2825-
// on at the testing frameworks layer? Instead of what we do now, which
2826-
// is check if a `jest` global is defined.
2827-
ReactCurrentActQueue.disableActWarning === false
2819+
ReactCurrentActQueue.current === null
28282820
) {
28292821
console.error(
28302822
'An update to %s ran an effect, but was not wrapped in act(...).\n\n' +
@@ -2846,15 +2838,8 @@ export function warnIfNotCurrentlyActingEffectsInDEV(fiber: Fiber): void {
28462838
function warnIfNotCurrentlyActingUpdatesInDEV(fiber: Fiber): void {
28472839
if (__DEV__) {
28482840
if (
2849-
warnsIfNotActing === true &&
28502841
executionContext === NoContext &&
2851-
ReactCurrentActQueue.current === null &&
2852-
// Our internal tests use a custom implementation of `act` that works by
2853-
// mocking the Scheduler package. Disable the `act` warning.
2854-
// TODO: Maybe the warning should be disabled by default, and then turned
2855-
// on at the testing frameworks layer? Instead of what we do now, which
2856-
// is check if a `jest` global is defined.
2857-
ReactCurrentActQueue.disableActWarning === false
2842+
ReactCurrentActQueue.current === null
28582843
) {
28592844
const previousFiber = ReactCurrentFiberCurrent;
28602845
try {

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ import {
8484
scheduleTimeout,
8585
cancelTimeout,
8686
noTimeout,
87-
warnsIfNotActing,
8887
afterActiveInstanceBlur,
8988
clearContainer,
9089
getCurrentEventPriority,
@@ -2816,15 +2815,8 @@ function shouldForceFlushFallbacksInDEV() {
28162815
export function warnIfNotCurrentlyActingEffectsInDEV(fiber: Fiber): void {
28172816
if (__DEV__) {
28182817
if (
2819-
warnsIfNotActing === true &&
28202818
(fiber.mode & StrictLegacyMode) !== NoMode &&
2821-
ReactCurrentActQueue.current === null &&
2822-
// Our internal tests use a custom implementation of `act` that works by
2823-
// mocking the Scheduler package. Disable the `act` warning.
2824-
// TODO: Maybe the warning should be disabled by default, and then turned
2825-
// on at the testing frameworks layer? Instead of what we do now, which
2826-
// is check if a `jest` global is defined.
2827-
ReactCurrentActQueue.disableActWarning === false
2819+
ReactCurrentActQueue.current === null
28282820
) {
28292821
console.error(
28302822
'An update to %s ran an effect, but was not wrapped in act(...).\n\n' +
@@ -2846,15 +2838,8 @@ export function warnIfNotCurrentlyActingEffectsInDEV(fiber: Fiber): void {
28462838
function warnIfNotCurrentlyActingUpdatesInDEV(fiber: Fiber): void {
28472839
if (__DEV__) {
28482840
if (
2849-
warnsIfNotActing === true &&
28502841
executionContext === NoContext &&
2851-
ReactCurrentActQueue.current === null &&
2852-
// Our internal tests use a custom implementation of `act` that works by
2853-
// mocking the Scheduler package. Disable the `act` warning.
2854-
// TODO: Maybe the warning should be disabled by default, and then turned
2855-
// on at the testing frameworks layer? Instead of what we do now, which
2856-
// is check if a `jest` global is defined.
2857-
ReactCurrentActQueue.disableActWarning === false
2842+
ReactCurrentActQueue.current === null
28582843
) {
28592844
const previousFiber = ReactCurrentFiberCurrent;
28602845
try {

0 commit comments

Comments
 (0)