Skip to content

Commit 9924aa7

Browse files
committed
Expose findDOMNode on internals
Exposes `findDOMNode` on internals and updates tests to read from internals
1 parent 8436bcc commit 9924aa7

26 files changed

+117
-73
lines changed

packages/react-dom/index.experimental.js

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export {
1212
createPortal,
1313
createRoot,
1414
hydrateRoot,
15-
findDOMNode,
1615
flushSync,
1716
unstable_batchedUpdates,
1817
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.

packages/react-dom/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export {
1414
createPortal,
1515
createRoot,
1616
hydrateRoot,
17-
findDOMNode,
1817
flushSync,
1918
render,
2019
unmountComponentAtNode,

packages/react-dom/index.stable.js

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export {
1212
createPortal,
1313
createRoot,
1414
hydrateRoot,
15-
findDOMNode,
1615
flushSync,
1716
render,
1817
unmountComponentAtNode,

packages/react-dom/src/ReactDOMSharedInternals.js

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @flow
88
*/
99

10+
import type {FindDOMNodeType} from './client/ReactDOMLegacy.js';
1011
import type {HostDispatcher} from './shared/ReactDOMTypes';
1112

1213
type InternalsType = {
@@ -15,6 +16,7 @@ type InternalsType = {
1516
ReactDOMCurrentDispatcher: {
1617
current: HostDispatcher,
1718
},
19+
findDOMNode: null | FindDOMNodeType,
1820
};
1921

2022
function noop() {}
@@ -35,6 +37,7 @@ const Internals: InternalsType = ({
3537
ReactDOMCurrentDispatcher: {
3638
current: DefaultDispatcher,
3739
},
40+
findDOMNode: null,
3841
}: any);
3942

4043
export default Internals;

packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ let React;
1515
let ReactDOM;
1616
let ReactDOMClient;
1717
let PropTypes;
18+
let findDOMNode;
1819

1920
const clone = function (o) {
2021
return JSON.parse(JSON.stringify(o));
@@ -95,6 +96,8 @@ describe('ReactComponentLifeCycle', () => {
9596

9697
React = require('react');
9798
ReactDOM = require('react-dom');
99+
findDOMNode =
100+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode;
98101
ReactDOMClient = require('react-dom/client');
99102
PropTypes = require('prop-types');
100103
});
@@ -383,7 +386,7 @@ describe('ReactComponentLifeCycle', () => {
383386
}
384387
render() {
385388
if (this.state.isMounted) {
386-
expect(ReactDOM.findDOMNode(this).tagName).toBe('DIV');
389+
expect(findDOMNode(this).tagName).toBe('DIV');
387390
}
388391
return <div />;
389392
}

packages/react-dom/src/__tests__/ReactDOM-test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
let React;
1313
let ReactDOM;
14+
let findDOMNode;
1415
let ReactDOMClient;
1516
let ReactDOMServer;
1617

@@ -21,6 +22,8 @@ describe('ReactDOM', () => {
2122
jest.resetModules();
2223
React = require('react');
2324
ReactDOM = require('react-dom');
25+
findDOMNode =
26+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode;
2427
ReactDOMClient = require('react-dom/client');
2528
ReactDOMServer = require('react-dom/server');
2629

@@ -494,7 +497,7 @@ describe('ReactDOM', () => {
494497
});
495498

496499
const App = () => {
497-
ReactDOM.findDOMNode(instance);
500+
findDOMNode(instance);
498501
return <div />;
499502
};
500503

packages/react-dom/src/__tests__/ReactDOMComponent-test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2121,7 +2121,11 @@ describe('ReactDOMComponent', () => {
21212121

21222122
componentWillUnmount() {
21232123
// Should not throw
2124-
expect(ReactDOM.findDOMNode(this).nodeName).toBe('SPAN');
2124+
expect(
2125+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
2126+
this,
2127+
).nodeName,
2128+
).toBe('SPAN');
21252129
}
21262130
}
21272131

packages/react-dom/src/__tests__/ReactDOMEventListener-test.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,18 @@ describe('ReactDOMEventListener', () => {
114114
this.setState({clicked: true});
115115
};
116116
componentDidMount() {
117-
expect(ReactDOM.findDOMNode(this)).toBe(container.firstChild);
117+
expect(
118+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
119+
this,
120+
),
121+
).toBe(container.firstChild);
118122
}
119123
componentDidUpdate() {
120-
expect(ReactDOM.findDOMNode(this)).toBe(container.firstChild);
124+
expect(
125+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
126+
this,
127+
),
128+
).toBe(container.firstChild);
121129
}
122130
render() {
123131
if (this.state.clicked) {

packages/react-dom/src/__tests__/ReactDOMLegacyFiber-test.js

+24-6
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ describe('ReactDOMLegacyFiber', () => {
111111
container,
112112
);
113113

114-
const textNode = ReactDOM.findDOMNode(instance);
114+
const textNode =
115+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
116+
instance,
117+
);
115118
expect(textNode).toBe(container.firstChild);
116119
expect(textNode.nodeType).toBe(3);
117120
expect(textNode.nodeValue).toBe('foo');
@@ -130,7 +133,10 @@ describe('ReactDOMLegacyFiber', () => {
130133

131134
expect(container.childNodes.length).toBe(2);
132135

133-
const firstNode = ReactDOM.findDOMNode(instance);
136+
const firstNode =
137+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
138+
instance,
139+
);
134140
expect(firstNode).toBe(container.firstChild);
135141
expect(firstNode.tagName).toBe('DIV');
136142
});
@@ -159,7 +165,10 @@ describe('ReactDOMLegacyFiber', () => {
159165

160166
expect(container.childNodes.length).toBe(2);
161167

162-
const firstNode = ReactDOM.findDOMNode(instance);
168+
const firstNode =
169+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
170+
instance,
171+
);
163172
expect(firstNode).toBe(container.firstChild);
164173
expect(firstNode.tagName).toBe('DIV');
165174
});
@@ -183,7 +192,10 @@ describe('ReactDOMLegacyFiber', () => {
183192

184193
expect(container.childNodes.length).toBe(2);
185194

186-
const firstNode = ReactDOM.findDOMNode(instance);
195+
const firstNode =
196+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
197+
instance,
198+
);
187199
expect(firstNode).toBe(container.firstChild);
188200
expect(firstNode.tagName).toBe('DIV');
189201
});
@@ -878,13 +890,19 @@ describe('ReactDOMLegacyFiber', () => {
878890
}
879891

880892
const myNodeA = ReactDOM.render(<MyNode />, container);
881-
const a = ReactDOM.findDOMNode(myNodeA);
893+
const a =
894+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
895+
myNodeA,
896+
);
882897
expect(a.tagName).toBe('DIV');
883898

884899
const myNodeB = ReactDOM.render(<MyNode flag={true} />, container);
885900
expect(myNodeA === myNodeB).toBe(true);
886901

887-
const b = ReactDOM.findDOMNode(myNodeB);
902+
const b =
903+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
904+
myNodeB,
905+
);
888906
expect(b.tagName).toBe('SPAN');
889907
});
890908

packages/react-dom/src/__tests__/ReactDOMSuspensePlaceholder-test.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
let React;
1313
let ReactDOM;
14+
let findDOMNode;
1415
let ReactDOMClient;
1516
let Suspense;
1617
let Scheduler;
@@ -24,6 +25,8 @@ describe('ReactDOMSuspensePlaceholder', () => {
2425
jest.resetModules();
2526
React = require('react');
2627
ReactDOM = require('react-dom');
28+
findDOMNode =
29+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode;
2730
ReactDOMClient = require('react-dom/client');
2831
Scheduler = require('scheduler');
2932
act = require('internal-test-utils').act;
@@ -232,11 +235,11 @@ describe('ReactDOMSuspensePlaceholder', () => {
232235
class Child extends React.Component {
233236
componentDidMount() {
234237
log.push('cDM ' + this.props.id);
235-
ReactDOM.findDOMNode(this);
238+
findDOMNode(this);
236239
}
237240
componentDidUpdate() {
238241
log.push('cDU ' + this.props.id);
239-
ReactDOM.findDOMNode(this);
242+
findDOMNode(this);
240243
}
241244
render() {
242245
return 'child';
@@ -291,12 +294,12 @@ describe('ReactDOMSuspensePlaceholder', () => {
291294
class Child extends React.Component {
292295
componentDidMount() {
293296
log.push('cDM');
294-
ReactDOM.findDOMNode(this);
297+
findDOMNode(this);
295298
}
296299

297300
componentDidUpdate() {
298301
log.push('cDU');
299-
ReactDOM.findDOMNode(this);
302+
findDOMNode(this);
300303
}
301304

302305
render() {

packages/react-dom/src/__tests__/ReactEmptyComponent-test.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
let React;
1313
let ReactDOM;
14+
let findDOMNode;
1415
let ReactDOMClient;
1516
let TogglingComponent;
1617
let act;
@@ -25,6 +26,8 @@ describe('ReactEmptyComponent', () => {
2526

2627
React = require('react');
2728
ReactDOM = require('react-dom');
29+
findDOMNode =
30+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode;
2831
ReactDOMClient = require('react-dom/client');
2932
Scheduler = require('scheduler');
3033
const InternalTestUtils = require('internal-test-utils');
@@ -37,12 +40,12 @@ describe('ReactEmptyComponent', () => {
3740
state = {component: this.props.firstComponent};
3841

3942
componentDidMount() {
40-
Scheduler.log('mount ' + ReactDOM.findDOMNode(this)?.nodeName);
43+
Scheduler.log('mount ' + findDOMNode(this)?.nodeName);
4144
this.setState({component: this.props.secondComponent});
4245
}
4346

4447
componentDidUpdate() {
45-
Scheduler.log('update ' + ReactDOM.findDOMNode(this)?.nodeName);
48+
Scheduler.log('update ' + findDOMNode(this)?.nodeName);
4649
}
4750

4851
render() {
@@ -244,13 +247,13 @@ describe('ReactEmptyComponent', () => {
244247
componentDidMount() {
245248
// Make sure the DOM node resolves properly even if we're replacing a
246249
// `null` component
247-
expect(ReactDOM.findDOMNode(this)).not.toBe(null);
250+
expect(findDOMNode(this)).not.toBe(null);
248251
}
249252

250253
componentWillUnmount() {
251254
// Even though we're getting replaced by `null`, we haven't been
252255
// replaced yet!
253-
expect(ReactDOM.findDOMNode(this)).not.toBe(null);
256+
expect(findDOMNode(this)).not.toBe(null);
254257
}
255258
}
256259

packages/react-dom/src/__tests__/ReactLegacyCompositeComponent-test.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
let React;
1313
let ReactDOM;
14+
let findDOMNode;
1415
let ReactDOMClient;
1516
let PropTypes;
1617
let act;
@@ -20,6 +21,8 @@ describe('ReactLegacyCompositeComponent', () => {
2021
jest.resetModules();
2122
React = require('react');
2223
ReactDOM = require('react-dom');
24+
findDOMNode =
25+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode;
2326
ReactDOMClient = require('react-dom/client');
2427
PropTypes = require('prop-types');
2528
act = require('internal-test-utils').act;
@@ -115,7 +118,7 @@ describe('ReactLegacyCompositeComponent', () => {
115118
await act(() => {
116119
root.render(<Parent ref={current => (component = current)} />);
117120
});
118-
expect(ReactDOM.findDOMNode(component).innerHTML).toBe('bar');
121+
expect(findDOMNode(component).innerHTML).toBe('bar');
119122
});
120123

121124
// @gate !disableLegacyContext
@@ -661,14 +664,14 @@ describe('ReactLegacyCompositeComponent', () => {
661664

662665
const container = document.createElement('div');
663666
const comp = ReactDOM.render(<Component flipped={false} />, container);
664-
expect(ReactDOM.findDOMNode(comp.static0Ref.current).textContent).toBe('A');
665-
expect(ReactDOM.findDOMNode(comp.static1Ref.current).textContent).toBe('B');
667+
expect(findDOMNode(comp.static0Ref.current).textContent).toBe('A');
668+
expect(findDOMNode(comp.static1Ref.current).textContent).toBe('B');
666669

667670
// When flipping the order, the refs should update even though the actual
668671
// contents do not
669672
ReactDOM.render(<Component flipped={true} />, container);
670-
expect(ReactDOM.findDOMNode(comp.static0Ref.current).textContent).toBe('B');
671-
expect(ReactDOM.findDOMNode(comp.static1Ref.current).textContent).toBe('A');
673+
expect(findDOMNode(comp.static0Ref.current).textContent).toBe('B');
674+
expect(findDOMNode(comp.static1Ref.current).textContent).toBe('A');
672675
});
673676

674677
// @gate !disableLegacyMode
@@ -678,12 +681,12 @@ describe('ReactLegacyCompositeComponent', () => {
678681

679682
class Component extends React.Component {
680683
componentDidMount() {
681-
a = ReactDOM.findDOMNode(this);
684+
a = findDOMNode(this);
682685
expect(a).not.toBe(null);
683686
}
684687

685688
componentWillUnmount() {
686-
b = ReactDOM.findDOMNode(this);
689+
b = findDOMNode(this);
687690
expect(b).not.toBe(null);
688691
}
689692

0 commit comments

Comments
 (0)