File tree Expand file tree Collapse file tree 6 files changed +51
-33
lines changed Expand file tree Collapse file tree 6 files changed +51
-33
lines changed Original file line number Diff line number Diff line change @@ -34,7 +34,6 @@ import {
34
34
createFiberFromText ,
35
35
createFiberFromPortal ,
36
36
} from './ReactFiber.new' ;
37
- import { emptyRefsObject } from './ReactFiberClassComponent.new' ;
38
37
import { isCompatibleFamilyForHotReloading } from './ReactFiberHotReloading.new' ;
39
38
import { StrictLegacyMode } from './ReactTypeOfMode' ;
40
39
import { getIsHydrating } from './ReactFiberHydrationContext.new' ;
@@ -199,11 +198,7 @@ function coerceRef(
199
198
return current . ref ;
200
199
}
201
200
const ref = function ( value ) {
202
- let refs = resolvedInst . refs ;
203
- if ( refs === emptyRefsObject ) {
204
- // This is a lazy pooled frozen object, so we need to initialize.
205
- refs = resolvedInst . refs = { } ;
206
- }
201
+ const refs = resolvedInst . refs ;
207
202
if ( value === null ) {
208
203
delete refs [ stringRef ] ;
209
204
} else {
Original file line number Diff line number Diff line change @@ -34,7 +34,6 @@ import {
34
34
createFiberFromText ,
35
35
createFiberFromPortal ,
36
36
} from './ReactFiber.old' ;
37
- import { emptyRefsObject } from './ReactFiberClassComponent.old' ;
38
37
import { isCompatibleFamilyForHotReloading } from './ReactFiberHotReloading.old' ;
39
38
import { StrictLegacyMode } from './ReactTypeOfMode' ;
40
39
import { getIsHydrating } from './ReactFiberHydrationContext.old' ;
@@ -199,11 +198,7 @@ function coerceRef(
199
198
return current . ref ;
200
199
}
201
200
const ref = function ( value ) {
202
- let refs = resolvedInst . refs ;
203
- if ( refs === emptyRefsObject ) {
204
- // This is a lazy pooled frozen object, so we need to initialize.
205
- refs = resolvedInst . refs = { } ;
206
- }
201
+ const refs = resolvedInst . refs ;
207
202
if ( value === null ) {
208
203
delete refs [ stringRef ] ;
209
204
} else {
Original file line number Diff line number Diff line change @@ -12,7 +12,6 @@ import type {Lanes} from './ReactFiberLane.new';
12
12
import type { UpdateQueue } from './ReactFiberClassUpdateQueue.new' ;
13
13
import type { Flags } from './ReactFiberFlags' ;
14
14
15
- import * as React from 'react' ;
16
15
import {
17
16
LayoutStatic ,
18
17
MountLayoutDev ,
@@ -82,10 +81,6 @@ import {
82
81
83
82
const fakeInternalInstance = { } ;
84
83
85
- // React.Component uses a shared frozen object by default.
86
- // We'll use it to determine whether we need to initialize legacy refs.
87
- export const emptyRefsObject = new React . Component ( ) . refs ;
88
-
89
84
let didWarnAboutStateAssignmentForComponent ;
90
85
let didWarnAboutUninitializedState ;
91
86
let didWarnAboutGetSnapshotBeforeUpdateWithoutDidUpdate ;
@@ -871,7 +866,6 @@ function mountClassInstance(
871
866
const instance = workInProgress . stateNode ;
872
867
instance . props = newProps ;
873
868
instance . state = workInProgress . memoizedState ;
874
- instance . refs = emptyRefsObject ;
875
869
876
870
initializeUpdateQueue ( workInProgress ) ;
877
871
Original file line number Diff line number Diff line change @@ -12,7 +12,6 @@ import type {Lanes} from './ReactFiberLane.old';
12
12
import type { UpdateQueue } from './ReactFiberClassUpdateQueue.old' ;
13
13
import type { Flags } from './ReactFiberFlags' ;
14
14
15
- import * as React from 'react' ;
16
15
import {
17
16
LayoutStatic ,
18
17
MountLayoutDev ,
@@ -82,10 +81,6 @@ import {
82
81
83
82
const fakeInternalInstance = { } ;
84
83
85
- // React.Component uses a shared frozen object by default.
86
- // We'll use it to determine whether we need to initialize legacy refs.
87
- export const emptyRefsObject = new React . Component ( ) . refs ;
88
-
89
84
let didWarnAboutStateAssignmentForComponent ;
90
85
let didWarnAboutUninitializedState ;
91
86
let didWarnAboutGetSnapshotBeforeUpdateWithoutDidUpdate ;
@@ -871,7 +866,6 @@ function mountClassInstance(
871
866
const instance = workInProgress . stateNode ;
872
867
instance . props = newProps ;
873
868
instance . state = workInProgress . memoizedState ;
874
- instance . refs = emptyRefsObject ;
875
869
876
870
initializeUpdateQueue ( workInProgress ) ;
877
871
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ * @emails react-core
8
+ */
9
+
10
+ 'use strict' ;
11
+
12
+ let React ;
13
+ let ReactNoop ;
14
+ let act ;
15
+
16
+ describe ( 'ReactFiberRefs' , ( ) => {
17
+ beforeEach ( ( ) => {
18
+ jest . resetModules ( ) ;
19
+ React = require ( 'react' ) ;
20
+ ReactNoop = require ( 'react-noop-renderer' ) ;
21
+ act = require ( 'jest-react' ) . act ;
22
+ } ) ;
23
+
24
+ test ( 'strings refs can be codemodded to callback refs' , async ( ) => {
25
+ let app ;
26
+ class App extends React . Component {
27
+ render ( ) {
28
+ app = this ;
29
+ return (
30
+ < div
31
+ prop = "Hello!"
32
+ ref = { el => {
33
+ // `refs` used to be a shared frozen object unless/until a string
34
+ // ref attached by the reconciler, but it's not anymore so that we
35
+ // can codemod string refs to userspace callback refs.
36
+ this . refs . div = el ;
37
+ } }
38
+ />
39
+ ) ;
40
+ }
41
+ }
42
+
43
+ const root = ReactNoop . createRoot ( ) ;
44
+ await act ( async ( ) => root . render ( < App /> ) ) ;
45
+ expect ( app . refs . div . prop ) . toBe ( 'Hello!' ) ;
46
+ } ) ;
47
+ } ) ;
Original file line number Diff line number Diff line change 8
8
import ReactNoopUpdateQueue from './ReactNoopUpdateQueue' ;
9
9
import assign from 'shared/assign' ;
10
10
11
- const emptyObject = { } ;
12
- if ( __DEV__ ) {
13
- Object . freeze ( emptyObject ) ;
14
- }
15
-
16
11
/**
17
12
* Base class helpers for the updating state of a component.
18
13
*/
19
14
function Component ( props , context , updater ) {
20
15
this . props = props ;
21
16
this . context = context ;
22
- // If a component has string refs, we will assign a different object later.
23
- this . refs = emptyObject ;
17
+ this . refs = { } ;
24
18
// We initialize the default updater but the real one gets injected by the
25
19
// renderer.
26
20
this . updater = updater || ReactNoopUpdateQueue ;
@@ -132,8 +126,7 @@ ComponentDummy.prototype = Component.prototype;
132
126
function PureComponent ( props , context , updater ) {
133
127
this . props = props ;
134
128
this . context = context ;
135
- // If a component has string refs, we will assign a different object later.
136
- this . refs = emptyObject ;
129
+ this . refs = { } ;
137
130
this . updater = updater || ReactNoopUpdateQueue ;
138
131
}
139
132
You can’t perform that action at this time.
0 commit comments