You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WARNING: Only apply this codemod if you've fixed all warnings like this:
149
+
150
+
```
151
+
Warning: Component "div" contains the string ref "inner". Support for string refs will be removed in a future major release. We recommend using useRef() or createRef() instead.
152
+
```
153
+
154
+
This codemod will convert deprecated string refs to callback refs.
155
+
156
+
Input:
157
+
158
+
```jsx
159
+
import*asReactfrom"react";
160
+
161
+
classParentComponentextendsReact.Component {
162
+
render() {
163
+
return<div ref="refComponent"/>;
164
+
}
165
+
}
166
+
```
167
+
168
+
Output:
169
+
170
+
```jsx
171
+
import*asReactfrom"react";
172
+
173
+
classParentComponentextendsReact.Component {
174
+
render() {
175
+
return (
176
+
<div
177
+
ref={(current) => {
178
+
this.refs["refComponent"] = current;
179
+
}}
180
+
/>
181
+
);
182
+
}
183
+
}
184
+
```
185
+
186
+
Note that this only works for string literals.
187
+
Referring to the ref with a variable will not trigger the transform:
188
+
Input:
189
+
190
+
```jsx
191
+
import*asReactfrom"react";
192
+
193
+
constrefName="refComponent";
194
+
195
+
classParentComponentextendsReact.Component {
196
+
render() {
197
+
return<div ref={refName} />;
198
+
}
199
+
}
200
+
```
201
+
202
+
Output (nothing changed):
203
+
204
+
```jsx
205
+
import*asReactfrom"react";
206
+
207
+
constrefName="refComponent";
208
+
209
+
classParentComponentextendsReact.Component {
210
+
render() {
211
+
return<div ref={refName} />;
212
+
}
213
+
}
214
+
```
215
+
146
216
#### `update-react-imports`
147
217
148
218
[As of Babel 7.9.0](https://babeljs.io/blog/2020/03/16/7.9.0#a-new-jsx-transform-11154-https-githubcom-babel-babel-pull-11154), when using `runtime: automatic` in `@babel/preset-react` or `@babel/plugin-transform-react-jsx`, you will not need to explicitly import React for compiling jsx. This codemod removes the redundant import statements. It also converts default imports (`import React from 'react'`) to named imports (e.g. `import { useState } from 'react'`).
'Reorders React component methods to match the ESLint react/sort-comp rule.',
189
189
value: 'sort-comp'
190
190
},
191
+
{
192
+
name:
193
+
'string-refs: Converts deprecated string refs to callback refs.',
194
+
value: 'string-refs'
195
+
},
191
196
{
192
197
name: 'update-react-imports: Removes redundant import statements from explicitly importing React to compile JSX and converts default imports to destructured named imports',
0 commit comments