Skip to content

Commit 85d4602

Browse files
committed
Expose findDOMNode on internals
Exposes `findDOMNode` on internals and updates tests to read from internals
1 parent a1ace9d commit 85d4602

26 files changed

+135
-78
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
hydrate,
1817
render,

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
hydrate,
2019
render,

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
hydrate,
1817
render,

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
Dispatcher: {
1617
current: null | HostDispatcher,
1718
},
19+
findDOMNode: null | FindDOMNodeType,
1820
};
1921

2022
const Internals: InternalsType = ({
@@ -23,6 +25,7 @@ const Internals: InternalsType = ({
2325
Dispatcher: {
2426
current: null,
2527
},
28+
findDOMNode: null,
2629
}: any);
2730

2831
export default Internals;

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ let ReactDOM;
1616
let ReactDOMClient;
1717
let ReactTestUtils;
1818
let PropTypes;
19+
let findDOMNode;
1920

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

9596
React = require('react');
9697
ReactDOM = require('react-dom');
98+
findDOMNode =
99+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode;
97100
ReactDOMClient = require('react-dom/client');
98101
ReactTestUtils = require('react-dom/test-utils');
99102
PropTypes = require('prop-types');
@@ -376,7 +379,7 @@ describe('ReactComponentLifeCycle', () => {
376379
}
377380
render() {
378381
if (this.state.isMounted) {
379-
expect(ReactDOM.findDOMNode(this).tagName).toBe('DIV');
382+
expect(findDOMNode(this).tagName).toBe('DIV');
380383
}
381384
return <div />;
382385
}

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
let ReactTestUtils;
@@ -21,6 +22,8 @@ describe('ReactDOM', () => {
2122
beforeEach(() => {
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
ReactTestUtils = require('react-dom/test-utils');
@@ -420,7 +423,7 @@ describe('ReactDOM', () => {
420423

421424
const instance = ReactTestUtils.renderIntoDocument(<Component />);
422425
const App = () => {
423-
ReactDOM.findDOMNode(instance);
426+
findDOMNode(instance);
424427
return <div />;
425428
};
426429

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

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

20362036
componentWillUnmount() {
20372037
// Should not throw
2038-
expect(ReactDOM.findDOMNode(this).nodeName).toBe('SPAN');
2038+
expect(
2039+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
2040+
this,
2041+
).nodeName,
2042+
).toBe('SPAN');
20392043
}
20402044
}
20412045

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,18 @@ describe('ReactDOMEventListener', () => {
110110
this.setState({clicked: true});
111111
};
112112
componentDidMount() {
113-
expect(ReactDOM.findDOMNode(this)).toBe(container.firstChild);
113+
expect(
114+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
115+
this,
116+
),
117+
).toBe(container.firstChild);
114118
}
115119
componentDidUpdate() {
116-
expect(ReactDOM.findDOMNode(this)).toBe(container.firstChild);
120+
expect(
121+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
122+
this,
123+
),
124+
).toBe(container.firstChild);
117125
}
118126
render() {
119127
if (this.state.clicked) {

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

+24-6
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ describe('ReactDOMLegacyFiber', () => {
103103
container,
104104
);
105105

106-
const textNode = ReactDOM.findDOMNode(instance);
106+
const textNode =
107+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
108+
instance,
109+
);
107110
expect(textNode).toBe(container.firstChild);
108111
expect(textNode.nodeType).toBe(3);
109112
expect(textNode.nodeValue).toBe('foo');
@@ -121,7 +124,10 @@ describe('ReactDOMLegacyFiber', () => {
121124

122125
expect(container.childNodes.length).toBe(2);
123126

124-
const firstNode = ReactDOM.findDOMNode(instance);
127+
const firstNode =
128+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
129+
instance,
130+
);
125131
expect(firstNode).toBe(container.firstChild);
126132
expect(firstNode.tagName).toBe('DIV');
127133
});
@@ -149,7 +155,10 @@ describe('ReactDOMLegacyFiber', () => {
149155

150156
expect(container.childNodes.length).toBe(2);
151157

152-
const firstNode = ReactDOM.findDOMNode(instance);
158+
const firstNode =
159+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
160+
instance,
161+
);
153162
expect(firstNode).toBe(container.firstChild);
154163
expect(firstNode.tagName).toBe('DIV');
155164
});
@@ -172,7 +181,10 @@ describe('ReactDOMLegacyFiber', () => {
172181

173182
expect(container.childNodes.length).toBe(2);
174183

175-
const firstNode = ReactDOM.findDOMNode(instance);
184+
const firstNode =
185+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
186+
instance,
187+
);
176188
expect(firstNode).toBe(container.firstChild);
177189
expect(firstNode.tagName).toBe('DIV');
178190
});
@@ -849,13 +861,19 @@ describe('ReactDOMLegacyFiber', () => {
849861
}
850862

851863
const myNodeA = ReactDOM.render(<MyNode />, container);
852-
const a = ReactDOM.findDOMNode(myNodeA);
864+
const a =
865+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
866+
myNodeA,
867+
);
853868
expect(a.tagName).toBe('DIV');
854869

855870
const myNodeB = ReactDOM.render(<MyNode flag={true} />, container);
856871
expect(myNodeA === myNodeB).toBe(true);
857872

858-
const b = ReactDOM.findDOMNode(myNodeB);
873+
const b =
874+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode(
875+
myNodeB,
876+
);
859877
expect(b.tagName).toBe('SPAN');
860878
});
861879

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;
@@ -23,6 +24,8 @@ describe('ReactDOMSuspensePlaceholder', () => {
2324
beforeEach(() => {
2425
React = require('react');
2526
ReactDOM = require('react-dom');
27+
findDOMNode =
28+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode;
2629
ReactDOMClient = require('react-dom/client');
2730
Scheduler = require('scheduler');
2831
act = require('internal-test-utils').act;
@@ -228,11 +231,11 @@ describe('ReactDOMSuspensePlaceholder', () => {
228231
class Child extends React.Component {
229232
componentDidMount() {
230233
log.push('cDM ' + this.props.id);
231-
ReactDOM.findDOMNode(this);
234+
findDOMNode(this);
232235
}
233236
componentDidUpdate() {
234237
log.push('cDU ' + this.props.id);
235-
ReactDOM.findDOMNode(this);
238+
findDOMNode(this);
236239
}
237240
render() {
238241
return 'child';
@@ -287,12 +290,12 @@ describe('ReactDOMSuspensePlaceholder', () => {
287290
class Child extends React.Component {
288291
componentDidMount() {
289292
log.push('cDM');
290-
ReactDOM.findDOMNode(this);
293+
findDOMNode(this);
291294
}
292295

293296
componentDidUpdate() {
294297
log.push('cDU');
295-
ReactDOM.findDOMNode(this);
298+
findDOMNode(this);
296299
}
297300

298301
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;
@@ -23,6 +24,8 @@ describe('ReactEmptyComponent', () => {
2324
beforeEach(() => {
2425
React = require('react');
2526
ReactDOM = require('react-dom');
27+
findDOMNode =
28+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode;
2629
ReactDOMClient = require('react-dom/client');
2730
Scheduler = require('scheduler');
2831
const InternalTestUtils = require('internal-test-utils');
@@ -35,12 +38,12 @@ describe('ReactEmptyComponent', () => {
3538
state = {component: this.props.firstComponent};
3639

3740
componentDidMount() {
38-
Scheduler.log('mount ' + ReactDOM.findDOMNode(this)?.nodeName);
41+
Scheduler.log('mount ' + findDOMNode(this)?.nodeName);
3942
this.setState({component: this.props.secondComponent});
4043
}
4144

4245
componentDidUpdate() {
43-
Scheduler.log('update ' + ReactDOM.findDOMNode(this)?.nodeName);
46+
Scheduler.log('update ' + findDOMNode(this)?.nodeName);
4447
}
4548

4649
render() {
@@ -242,13 +245,13 @@ describe('ReactEmptyComponent', () => {
242245
componentDidMount() {
243246
// Make sure the DOM node resolves properly even if we're replacing a
244247
// `null` component
245-
expect(ReactDOM.findDOMNode(this)).not.toBe(null);
248+
expect(findDOMNode(this)).not.toBe(null);
246249
}
247250

248251
componentWillUnmount() {
249252
// Even though we're getting replaced by `null`, we haven't been
250253
// replaced yet!
251-
expect(ReactDOM.findDOMNode(this)).not.toBe(null);
254+
expect(findDOMNode(this)).not.toBe(null);
252255
}
253256
}
254257

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;
@@ -19,6 +20,8 @@ describe('ReactLegacyCompositeComponent', () => {
1920
beforeEach(() => {
2021
React = require('react');
2122
ReactDOM = require('react-dom');
23+
findDOMNode =
24+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode;
2225
ReactDOMClient = require('react-dom/client');
2326
PropTypes = require('prop-types');
2427
act = require('internal-test-utils').act;
@@ -113,7 +116,7 @@ describe('ReactLegacyCompositeComponent', () => {
113116
await act(() => {
114117
root.render(<Parent ref={current => (component = current)} />);
115118
});
116-
expect(ReactDOM.findDOMNode(component).innerHTML).toBe('bar');
119+
expect(findDOMNode(component).innerHTML).toBe('bar');
117120
});
118121

119122
// @gate !disableLegacyContext
@@ -654,14 +657,14 @@ describe('ReactLegacyCompositeComponent', () => {
654657

655658
const container = document.createElement('div');
656659
const comp = ReactDOM.render(<Component flipped={false} />, container);
657-
expect(ReactDOM.findDOMNode(comp.static0Ref.current).textContent).toBe('A');
658-
expect(ReactDOM.findDOMNode(comp.static1Ref.current).textContent).toBe('B');
660+
expect(findDOMNode(comp.static0Ref.current).textContent).toBe('A');
661+
expect(findDOMNode(comp.static1Ref.current).textContent).toBe('B');
659662

660663
// When flipping the order, the refs should update even though the actual
661664
// contents do not
662665
ReactDOM.render(<Component flipped={true} />, container);
663-
expect(ReactDOM.findDOMNode(comp.static0Ref.current).textContent).toBe('B');
664-
expect(ReactDOM.findDOMNode(comp.static1Ref.current).textContent).toBe('A');
666+
expect(findDOMNode(comp.static0Ref.current).textContent).toBe('B');
667+
expect(findDOMNode(comp.static1Ref.current).textContent).toBe('A');
665668
});
666669

667670
it('should allow access to findDOMNode in componentWillUnmount in legacy mode', () => {
@@ -670,12 +673,12 @@ describe('ReactLegacyCompositeComponent', () => {
670673

671674
class Component extends React.Component {
672675
componentDidMount() {
673-
a = ReactDOM.findDOMNode(this);
676+
a = findDOMNode(this);
674677
expect(a).not.toBe(null);
675678
}
676679

677680
componentWillUnmount() {
678-
b = ReactDOM.findDOMNode(this);
681+
b = findDOMNode(this);
679682
expect(b).not.toBe(null);
680683
}
681684

0 commit comments

Comments
 (0)