File tree Expand file tree Collapse file tree 4 files changed +49
-19
lines changed Expand file tree Collapse file tree 4 files changed +49
-19
lines changed Original file line number Diff line number Diff line change @@ -82,10 +82,6 @@ import {
82
82
83
83
const fakeInternalInstance = { } ;
84
84
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
85
let didWarnAboutStateAssignmentForComponent ;
90
86
let didWarnAboutUninitializedState ;
91
87
let didWarnAboutGetSnapshotBeforeUpdateWithoutDidUpdate ;
@@ -871,7 +867,6 @@ function mountClassInstance(
871
867
const instance = workInProgress . stateNode ;
872
868
instance . props = newProps ;
873
869
instance . state = workInProgress . memoizedState ;
874
- instance . refs = emptyRefsObject ;
875
870
876
871
initializeUpdateQueue ( workInProgress ) ;
877
872
Original file line number Diff line number Diff line change @@ -82,10 +82,6 @@ import {
82
82
83
83
const fakeInternalInstance = { } ;
84
84
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
85
let didWarnAboutStateAssignmentForComponent ;
90
86
let didWarnAboutUninitializedState ;
91
87
let didWarnAboutGetSnapshotBeforeUpdateWithoutDidUpdate ;
@@ -871,7 +867,6 @@ function mountClassInstance(
871
867
const instance = workInProgress . stateNode ;
872
868
instance . props = newProps ;
873
869
instance . state = workInProgress . memoizedState ;
874
- instance . refs = emptyRefsObject ;
875
870
876
871
initializeUpdateQueue ( workInProgress ) ;
877
872
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