Skip to content

Commit f5ce642

Browse files
authoredApr 19, 2024··
Revert "Support writing to this.refs from userspace" (#28877)
Reverts #28867 It broke some tests, reverting until we figure out why to avoid having too much delay in the sync.
1 parent 33a3244 commit f5ce642

File tree

3 files changed

+9
-26
lines changed

3 files changed

+9
-26
lines changed
 

‎packages/react-reconciler/src/ReactFiberClassComponent.js

+1
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ function mountClassInstance(
819819
const instance = workInProgress.stateNode;
820820
instance.props = newProps;
821821
instance.state = workInProgress.memoizedState;
822+
instance.refs = {};
822823

823824
initializeUpdateQueue(workInProgress);
824825

‎packages/react-reconciler/src/__tests__/ReactFiberRefs-test.js

-24
Original file line numberDiff line numberDiff line change
@@ -138,28 +138,4 @@ describe('ReactFiberRefs', () => {
138138
);
139139
expect(refProp).toBe('child');
140140
});
141-
142-
test('strings refs can be codemodded to callback refs', async () => {
143-
let app;
144-
class App extends React.Component {
145-
render() {
146-
app = this;
147-
return (
148-
<div
149-
prop="Hello!"
150-
ref={el => {
151-
// `refs` used to be a shared frozen object unless/until a string
152-
// ref attached by the reconciler, but it's not anymore so that we
153-
// can codemod string refs to userspace callback refs.
154-
this.refs.div = el;
155-
}}
156-
/>
157-
);
158-
}
159-
}
160-
161-
const root = ReactNoop.createRoot();
162-
await act(() => root.render(<App />));
163-
expect(app.refs.div.prop).toBe('Hello!');
164-
});
165141
});

‎packages/react/src/ReactBaseClasses.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88
import ReactNoopUpdateQueue from './ReactNoopUpdateQueue';
99
import assign from 'shared/assign';
1010

11+
const emptyObject = {};
12+
if (__DEV__) {
13+
Object.freeze(emptyObject);
14+
}
15+
1116
/**
1217
* Base class helpers for the updating state of a component.
1318
*/
1419
function Component(props, context, updater) {
1520
this.props = props;
1621
this.context = context;
17-
this.refs = {};
22+
// If a component has string refs, we will assign a different object later.
23+
this.refs = emptyObject;
1824
// We initialize the default updater but the real one gets injected by the
1925
// renderer.
2026
this.updater = updater || ReactNoopUpdateQueue;
@@ -127,7 +133,7 @@ function PureComponent(props, context, updater) {
127133
this.props = props;
128134
this.context = context;
129135
// If a component has string refs, we will assign a different object later.
130-
this.refs = {};
136+
this.refs = emptyObject;
131137
this.updater = updater || ReactNoopUpdateQueue;
132138
}
133139

0 commit comments

Comments
 (0)
Please sign in to comment.