Skip to content

Commit

Permalink
Support writing to this.refs from userspace
Browse files Browse the repository at this point in the history
Previously, the `refs` property of a class component instance was
read-only by user code — only React could write to it, and until/unless
a string ref was used, it pointed to a shared empty object that was
frozen in dev to prevent userspace mutations.

Because string refs are deprecated, we want users to be able to codemod
all their string refs to callback refs. The safest way to do this is to
output a callback ref that assigns to `this.refs`.

So to support this, we need to make `this.refs` writable by userspace.
  • Loading branch information
acdlite committed Apr 18, 2024
1 parent 0061ca6 commit 773bb4a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
1 change: 0 additions & 1 deletion packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,6 @@ function mountClassInstance(
const instance = workInProgress.stateNode;
instance.props = newProps;
instance.state = workInProgress.memoizedState;
instance.refs = {};

initializeUpdateQueue(workInProgress);

Expand Down
24 changes: 24 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactFiberRefs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,28 @@ describe('ReactFiberRefs', () => {
);
expect(refProp).toBe('child');
});

test('strings refs can be codemodded to callback refs', async () => {
let app;
class App extends React.Component {
render() {
app = this;
return (
<div
prop="Hello!"
ref={el => {
// `refs` used to be a shared frozen object unless/until a string
// ref attached by the reconciler, but it's not anymore so that we
// can codemod string refs to userspace callback refs.
this.refs.div = el;
}}
/>
);
}
}

const root = ReactNoop.createRoot();
await act(() => root.render(<App />));
expect(app.refs.div.prop).toBe('Hello!');
});
});
18 changes: 6 additions & 12 deletions packages/react/src/ReactBaseClasses.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@
import ReactNoopUpdateQueue from './ReactNoopUpdateQueue';
import assign from 'shared/assign';

const emptyObject = {};
if (__DEV__) {
Object.freeze(emptyObject);
}

/**
* Base class helpers for the updating state of a component.
*/
function Component(props, context, updater) {
this.props = props;
this.context = context;
// If a component has string refs, we will assign a different object later.
this.refs = emptyObject;
this.refs = {};
// We initialize the default updater but the real one gets injected by the
// renderer.
this.updater = updater || ReactNoopUpdateQueue;
Expand Down Expand Up @@ -53,7 +47,7 @@ Component.prototype.isReactComponent = {};
* @final
* @protected
*/
Component.prototype.setState = function (partialState, callback) {
Component.prototype.setState = function(partialState, callback) {
if (
typeof partialState !== 'object' &&
typeof partialState !== 'function' &&
Expand Down Expand Up @@ -82,7 +76,7 @@ Component.prototype.setState = function (partialState, callback) {
* @final
* @protected
*/
Component.prototype.forceUpdate = function (callback) {
Component.prototype.forceUpdate = function(callback) {
this.updater.enqueueForceUpdate(this, callback, 'forceUpdate');
};

Expand All @@ -104,9 +98,9 @@ if (__DEV__) {
'https://github.com/facebook/react/issues/3236).',
],
};
const defineDeprecationWarning = function (methodName, info) {
const defineDeprecationWarning = function(methodName, info) {
Object.defineProperty(Component.prototype, methodName, {
get: function () {
get: function() {
console.warn(
'%s(...) is deprecated in plain JavaScript React classes. %s',
info[0],
Expand All @@ -133,7 +127,7 @@ function PureComponent(props, context, updater) {
this.props = props;
this.context = context;
// If a component has string refs, we will assign a different object later.
this.refs = emptyObject;
this.refs = {};
this.updater = updater || ReactNoopUpdateQueue;
}

Expand Down

0 comments on commit 773bb4a

Please sign in to comment.