WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit e591d1c

Browse files
authored
fix(react): fix dev-only validation to work in React 19 (#723)
* fix(react): fix dev-only validation to work in React 19 * Change files
1 parent a4c3561 commit e591d1c

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "fix: fix dev-only validation to work in React 19",
4+
"packageName": "@griffel/react",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/react/src/createDOMRenderer.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { createDOMRenderer, mergeClasses } from '@griffel/core';
22
import * as React from 'react';
33
import { hydrateRoot } from 'react-dom/client';
44
import { renderToStaticMarkup } from 'react-dom/server';
5-
import { act } from 'react-dom/test-utils';
65

76
import { makeStyles } from './makeStyles';
87
import { makeResetStyles } from './makeResetStyles';
@@ -100,7 +99,7 @@ describe('createDOMRenderer', () => {
10099
jest.spyOn(styleEl.sheet!, 'insertRule'),
101100
);
102101

103-
act(() => {
102+
React.act(() => {
104103
hydrateRoot(
105104
container,
106105
// "RendererProvider" is not required there, we need it only for Jest spies

packages/react/src/renderToStyleElements.test.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as prettier from 'prettier';
44
import * as React from 'react';
55
import { createRoot } from 'react-dom/client';
66
import { renderToStaticMarkup } from 'react-dom/server';
7-
import { act } from 'react-dom/test-utils';
87

98
import { makeStyles } from './makeStyles';
109
import { makeResetStyles } from './makeResetStyles';
@@ -48,7 +47,7 @@ describe('renderToStyleElements (DOM)', () => {
4847
};
4948
const root = createRoot(document.createElement('div'));
5049

51-
act(() => {
50+
React.act(() => {
5251
root.render(
5352
<RendererProvider renderer={renderer}>
5453
<ExampleComponent />
@@ -87,7 +86,7 @@ describe('renderToStyleElements (DOM)', () => {
8786
};
8887
const root = createRoot(document.createElement('div'));
8988

90-
act(() => {
89+
React.act(() => {
9190
root.render(
9291
<RendererProvider renderer={renderer}>
9392
<ExampleComponent />

packages/react/src/utils/isInsideComponent.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@
22

33
import * as React from 'react';
44

5+
function getDispatcher() {
6+
try {
7+
return (React as any)[''.concat('__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE')].H;
8+
} catch {
9+
// React 19+
10+
}
11+
12+
try {
13+
return (React as any)[''.concat('__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED')].ReactCurrentDispatcher
14+
.current;
15+
} catch {
16+
// React 18 and below
17+
}
18+
}
19+
520
export function isInsideComponent() {
621
// React 18 always logs errors if a dispatcher is not present:
722
// https://github.com/facebook/react/blob/42f15b324f50d0fd98322c21646ac3013e30344a/packages/react/src/ReactHooks.js#L26-L36
823
try {
9-
// @ts-expect-error "SECRET_INTERNALS" are not typed
10-
const dispatcher = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher.current;
24+
const dispatcher = getDispatcher();
1125

1226
// Before any React component was rendered "dispatcher" will be "null"
1327
if (dispatcher === null || dispatcher === undefined) {

0 commit comments

Comments
 (0)