1
1
## react-codemod [ ![ Build Status] ( https://travis-ci.org/reactjs/react-codemod.svg )] ( https://travis-ci.org/reactjs/react-codemod )
2
2
3
- This repository contains a collection of codemod scripts for use with
4
- [ JSCodeshift] ( https://github.com/facebook/jscodeshift ) that help update React
5
- APIs.
3
+ This repository contains a collection of codemods to help update React apps.
6
4
7
5
### Usage
6
+ There are two supported ways to run React codemods:
7
+ - ` codemod ` : enhanced codemod support from [ codemod.com] ( https://codemod.com ) available via CLI or the [ VSCode Extension] ( https://go.codemod.com/vsce ) .
8
+ - ` react-codemod ` : basic codemod support using [ JSCodeshift] ( https://github.com/facebook/jscodeshift ) .
9
+
10
+ The ` codemod.com ` team maintains many of the codemods listed here, and all codemods are free and Open Source whether you use the ` codemod ` command or ` react-codemod ` .
11
+
12
+ #### Running with ` codemod ` CLI
13
+ ` npx codemod <framework>/<version>/<transform> --target <path> [...options] `
14
+ * ` transform ` - name of transform, see available transforms below.
15
+ * ` path ` - directory to transform
16
+
17
+ Check [ codemod docs] ( https://go.codemod.com/cli-docs] ) for the full list of available commands.
18
+
19
+ #### Running with ` react-codemod `
8
20
` npx react-codemod <transform> <path> [...options] `
9
21
* ` transform ` - name of transform, see available transforms below.
10
22
* ` path ` - files or directory to transform
11
23
* use the ` --dry ` option for a dry-run and use ` --print ` to print the output for comparison
12
24
13
25
This will start an interactive wizard, and then run the specified transform.
14
- #### Running with ` codemod ` CLI
15
- ` npx codemod <framework>/<version>/<transform> --target <path> [...options] `
16
- * ` transform ` - name of transform, see available transforms below.
17
- * ` path ` - directory to transform
18
-
19
- Check [ codemod docs] ( https://go.codemod.com/cli-docs] ) for the full list of available commands.
20
26
21
27
### Included Transforms
22
28
23
29
#### ` remove-context-provider `
24
30
25
31
Converts ` Context.Provider ` JSX opening and closing elements into ` Context ` .
26
32
27
- Run with ` react-codemod ` CLI for basic experience:
28
-
33
+ ` codemod ` :
29
34
``` sh
30
- npx react- codemod remove-context-provider < path>
35
+ npx codemod react/19/ remove-context-provider --target < path>
31
36
```
32
37
33
- or use ` codemod ` CLI for better experience and support:
34
-
38
+ ` react-codemod ` :
35
39
``` sh
36
- npx codemod react/19/ remove-context-provider --target < path>
40
+ npx react- codemod remove-context-provider < path>
37
41
```
38
42
39
43
#### ` remove-forward-ref `
40
44
41
45
Removes usages of ` forwardRef ` .
42
46
43
- Run with ` react-codemod ` CLI for basic experience:
44
-
47
+ ` codemod ` :
45
48
``` sh
46
- npx react- codemod remove-forward-ref < path>
49
+ npx codemod react/19/ remove-forward-ref --target < path>
47
50
```
48
51
49
- or use ` codemod ` CLI for better experience and support:
50
-
52
+ ` react-codemod ` :
51
53
``` sh
52
- npx codemod react/19/ remove-forward-ref --target < path>
54
+ npx react- codemod remove-forward-ref < path>
53
55
```
54
56
55
57
#### ` remove-memoization-hooks `
56
58
57
59
Removes usages of ` useCallback ` , ` useMemo ` and ` memo ` .
58
60
59
- Run with ` react- codemod` CLI for basic experience :
61
+ ` codemod ` :
60
62
61
63
``` sh
62
- npx react- codemod remove-memoization-hooks < path>
64
+ npx codemod react/19/ remove-memoization-hooks --target < path>
63
65
```
64
66
65
- or use ` codemod ` CLI for better experience and support:
66
-
67
+ ` react-codemod ` :
67
68
``` sh
68
- npx codemod react/19/ remove-memoization-hooks --target < path>
69
+ npx react- codemod remove-memoization-hooks < path>
69
70
```
70
71
71
72
#### ` use-context-hook `
72
73
73
74
Replaces usages of ` React.useContext(...) ` with ` React.use(...) ` .
74
75
75
- Run with ` react- codemod` CLI for basic experience :
76
+ ` codemod ` :
76
77
77
78
``` sh
78
- npx react- codemod use-context-hook < path>
79
+ npx codemod react/19/ use-context-hook --target < path>
79
80
```
80
81
81
- or use ` codemod ` CLI for better experience and support :
82
+ ` react- codemod` :
82
83
83
84
``` sh
84
- npx codemod react/19/ use-context-hook --target < path>
85
+ npx react- codemod use-context-hook < path>
85
86
```
86
87
87
88
#### ` replace-act-import `
88
89
89
90
Updates ` act ` import path from ` react-dom/test-utils ` to ` react ` .
90
91
91
- Run with ` react-codemod ` CLI for basic experience:
92
-
92
+ ` codemod ` :
93
93
``` sh
94
- npx react- codemod replace-act-import < path>
94
+ npx codemod react/19/ replace-act-import --target < path>
95
95
```
96
96
97
- or use ` codemod ` CLI for better experience and support:
98
-
97
+ ` react-codemod ` :
99
98
``` sh
100
- npx codemod react/19/ replace-act-import --target < path>
99
+ npx react- codemod replace-act-import < path>
101
100
```
102
101
103
102
#### ` replace-string-ref `
104
103
105
104
Replaces deprecated string refs with callback refs.
106
105
107
- Run with ` react-codemod ` CLI for basic experience:
108
-
106
+ ` codemod ` :
109
107
``` sh
110
- npx react- codemod replace-string-ref < path>
108
+ npx codemod react/19/ replace-string-ref --target < path>
111
109
```
112
110
113
- or use ` codemod ` CLI for better experience and support:
114
-
111
+ ` react-codemod ` :
115
112
``` sh
116
- npx codemod react/19/ replace-string-ref --target < path>
113
+ npx react- codemod replace-string-ref < path>
117
114
```
118
115
119
116
#### ` replace-use-form-state `
120
117
121
118
Replaces usages of useFormState() to use useActionState().
122
119
123
- Run with ` react-codemod ` CLI for basic experience:
124
-
120
+ ` codemod ` :
125
121
``` sh
126
- npx react- codemod replace-use-form-state < path>
122
+ npx codemod react/19/ replace-use-form-state --target < path>
127
123
```
128
124
129
- or use ` codemod ` CLI for better experience and support:
130
-
125
+ ` react-codemod ` :
131
126
``` sh
132
- npx codemod react/19/ replace-use-form-state --target < path>
127
+ npx react- codemod replace-use-form-state < path>
133
128
```
134
129
135
130
#### ` create-element-to-jsx `
@@ -313,10 +308,8 @@ npx react-codemod update-react-imports <path>
313
308
- Adds a ` require ` or ` import ` statement for ` create-react-class ` . The import style is inferred from the import style of the ` react ` import. The default module name can be overridden with the ` --create-class-module-name ` option.
314
309
- Prunes the ` react ` import if there are no more references to it.
315
310
316
- #### Usage
317
- ``` bash
318
- npx react-codemod class < path>
319
- ```
311
+
312
+ ## ` react-codemod ` options
320
313
321
314
### jscodeshift options
322
315
@@ -336,11 +329,15 @@ through jscodeshift's `printOptions` command line argument
336
329
npx react-codemod < transform> < path> --jscodeshift=" --printOptions='{\" quote\" :\" double\" }'"
337
330
```
338
331
339
- #### ` explicit-require=false `
332
+ ### ` explicit-require=false `
340
333
341
334
If you're not explicitly importing React in your files (eg: if you're loading React with a script tag), you should add ` --explicit-require=false ` .
342
335
343
- ### Support and Contributing
336
+ ## ` codemod ` options
337
+
338
+ Check [ codemod docs] ( https://go.codemod.com/cli-docs] ) for the full list of available options.
339
+
340
+ ## Support and Contributing
344
341
345
342
The scripts in this repository are provided in the hope that they are useful,
346
343
but they are not officially maintained, and we generally will not fix
@@ -350,7 +347,6 @@ rely on community contributions to fix any issues discovered or make any
350
347
improvements. If you want to contribute, you're welcome to submit a pull
351
348
request.
352
349
353
- ### License
354
-
350
+ ## License
355
351
356
352
react-codemod is [ MIT licensed] ( ./LICENSE ) .
0 commit comments