Skip to content

Commit

Permalink
Revert "Unseal this.refs"
Browse files Browse the repository at this point in the history
This reverts commit 72bc471.

We'll just not seal `this.refs` in React versions with `warnAboutStringRefs`.
  • Loading branch information
eps1lon committed Apr 24, 2024
1 parent 4d37e63 commit 5376b3b
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 118 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,6 @@ class ParentComponent extends React.Component {
return (
<div
ref={(current) => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs["refComponent"] = current;
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,19 @@ class ParentComponent extends React.Component {
render() {
return (
<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P'] = current;
}} id="P">
<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_P1'] = current;
}} id="P_P1">
<span ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_P1_C1'] = current;
}} id="P_P1_C1" />
<span ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_P1_C2'] = current;
}} id="P_P1_C2" />
</div>
<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_OneOff'] = current;
}} id="P_OneOff" />
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import * as React from "react";

<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['bad'] = current;
}} />;
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,19 @@ class ParentComponent extends React.Component {
render() {
return (
<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P'] = current;
}} id="P">
<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_P1'] = current;
}} id="P_P1">
<span ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_P1_C1'] = current;
}} id="P_P1_C1" />
<span ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_P1_C2'] = current;
}} id="P_P1_C2" />
</div>
<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_OneOff'] = current;
}} id="P_OneOff" />
</div>
Expand Down
47 changes: 1 addition & 46 deletions transforms/string-refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,56 +40,11 @@ export default (file, api, options) => {
// Maybe JSCodeShift has such a helper as well?
const currentIdentifierName = "current";
valuePath.replace(
// {(current) => {}}
// {(current) => { this.refs[valuePath.node.value] = current }}
j.jsxExpressionContainer(
j.arrowFunctionExpression(
[j.identifier(currentIdentifierName)],
j.blockStatement([
// if (process.env.NODE_ENV !== 'production')
j.ifStatement(
j.binaryExpression(
"!==",
j.memberExpression(
j.memberExpression(
j.identifier("process"),
j.identifier("env")
),
j.identifier("NODE_ENV")
),
j.stringLiteral("production")
),
j.blockStatement([
// if (Object.isSealed(this.refs))
j.ifStatement(
j.callExpression(
j.memberExpression(
j.identifier("Object"),
j.identifier("isSealed")
),
[
j.memberExpression(
j.thisExpression(),
j.identifier("refs")
),
]
),
j.blockStatement([
// this.refs = {}
j.expressionStatement(
j.assignmentExpression(
"=",
j.memberExpression(
j.thisExpression(),
j.identifier("refs")
),
j.objectExpression([])
)
),
])
),
])
),
// this.refs[valuePath.node.value] = current
j.expressionStatement(
j.assignmentExpression(
"=",
Expand Down

0 comments on commit 5376b3b

Please sign in to comment.