Skip to content

Commit 5ed190b

Browse files
authored
feat(codemod): add codemod that replaces the react-query import specifiers (#3801)
* feat(codemod): add codemod that replaces the react-query import specifiers * feat(codemod): add codemod that replaces the react-query import specifiers update migration docs * feat(codemod): add codemod that replaces the react-query import specifiers re-add yarn installation instructions
1 parent f7cfe5f commit 5ed190b

File tree

7 files changed

+98
-1
lines changed

7 files changed

+98
-1
lines changed

docs/devtools.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ The devtools are a separate package that you need to install:
1515

1616
```bash
1717
$ npm i @tanstack/react-query-devtools
18+
# or
19+
$ yarn add @tanstack/react-query-devtools
1820
```
1921

2022
You can import the devtools like this:

docs/guides/migrating-to-react-query-4.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ title: Migrating to React Query 4
55

66
## Breaking Changes
77

8+
v4 is a major version, so there are some breaking changes to be aware of:
9+
810
### react-query is now @tanstack/react-query
911

1012
```diff
@@ -15,6 +17,32 @@ title: Migrating to React Query 4
1517
+ import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
1618
```
1719

20+
#### Codemod
21+
22+
To make this migration easier, v4 comes with a codemod.
23+
24+
> The codemod is a best efforts attempt to help you migrate the breaking change. Please review the generated code thoroughly! Also, there are edge cases that cannot be found by the code mod, so please keep an eye on the log output.
25+
26+
You can easily apply it by using one (or both) of the following commands:
27+
28+
If you want to run it against `.js` or `.jsx` files, please use the command below:
29+
30+
```
31+
npx jscodeshift --extensions=js,jsx --transform=./node_modules/@tanstack/react-query/codemods/v4/replace-import-specifier.js ./path/to/src/
32+
```
33+
34+
If you want to run it against `.ts` or `.tsx` files, please use the command below:
35+
36+
```
37+
npx jscodeshift --extensions=ts,tsx --parser=tsx --transform=./node_modules/@tanstack/react-query/codemods/v4/replace-import-specifier.js ./path/to/src/
38+
```
39+
40+
Please note in the case of `TypeScript` you need to use `tsx` as the parser; otherwise, the codemod won't be applied properly!
41+
42+
**Note:** Applying the codemod might break your code formatting, so please don't forget to run `prettier` and/or `eslint` after you've applied the codemod!
43+
44+
**Note:** The codemod will _only_ change the imports - you still have to install the separate devtools package manually.
45+
1846
### Query Keys (and Mutation Keys) need to be an Array
1947

2048
In v3, Query and Mutation Keys could be a String or an Array. Internally, React Query has always worked with Array Keys only, and we've sometimes exposed this to consumers. For example, in the `queryFn`, you would always get the key as an Array to make working with [Default Query Functions](./default-query-function) easier.
@@ -48,7 +76,7 @@ If you want to run it against `.ts` or `.tsx` files, please use the command belo
4876
npx jscodeshift --extensions=ts,tsx --parser=tsx --transform=./node_modules/@tanstack/react-query/codemods/v4/key-transformation.js ./path/to/src/
4977
```
5078

51-
Please note in the case of `TypeScript` you need to use `tsx` as the parser otherwise, the codemod won't be applied properly!
79+
Please note in the case of `TypeScript` you need to use `tsx` as the parser; otherwise, the codemod won't be applied properly!
5280

5381
**Note:** Applying the codemod might break your code formatting, so please don't forget to run `prettier` and/or `eslint` after you've applied the codemod!
5482

@@ -335,6 +363,12 @@ If you were importing anything from `'react-query/react'` directly in your proje
335363

336364
## New Features 🚀
337365

366+
v4 comes with an awesome set of new features:
367+
368+
### Support for React 18
369+
370+
React 18 was released earlier this year, and v4 now has first class support for it and the new concurrent features it brings.
371+
338372
### Proper offline support
339373

340374
In v3, React Query has always fired off queries and mutations, but then taken the assumption that if you want to retry it, you need to be connected to the internet. This has led to several confusing situations:

docs/installation.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ or a good ol' `<script>` via
1111

1212
```bash
1313
$ npm i @tanstack/react-query
14+
# or
15+
$ yarn add @tanstack/react-query
1416
```
1517

1618
React Query is compatible with React v16.8+ and works with ReactDOM and React Native.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// React Query
2+
import { useQuery, useQueryClient } from 'react-query'
3+
import { useQuery as RenamedUseQuery, useQueryClient as RenamedUseQueryClient } from 'react-query'
4+
import DefaultReactQuery from 'react-query'
5+
import * as NamespacedReactQuery from 'react-query'
6+
// Devtools
7+
import { ReactQueryDevtools } from 'react-query/devtools'
8+
import { ReactQueryDevtools as RenamedReactQueryDevtools } from 'react-query/devtools'
9+
import DefaultReactQueryDevtools from 'react-query/devtools'
10+
import * as NamespacedReactQueryDevtools from 'react-query/devtools'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// React Query
2+
import { useQuery, useQueryClient } from '@tanstack/react-query'
3+
import { useQuery as RenamedUseQuery, useQueryClient as RenamedUseQueryClient } from '@tanstack/react-query'
4+
import DefaultReactQuery from '@tanstack/react-query'
5+
import * as NamespacedReactQuery from '@tanstack/react-query'
6+
// Devtools
7+
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
8+
import { ReactQueryDevtools as RenamedReactQueryDevtools } from '@tanstack/react-query-devtools'
9+
import DefaultReactQueryDevtools from '@tanstack/react-query-devtools'
10+
import * as NamespacedReactQueryDevtools from '@tanstack/react-query-devtools'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// eslint-disable-next-line @typescript-eslint/no-var-requires
2+
const defineTest = require('jscodeshift/dist/testUtils').defineTest
3+
4+
jest.autoMockOff()
5+
6+
defineTest(
7+
__dirname,
8+
'replace-import-specifier',
9+
null,
10+
'replace-import-specifier',
11+
{
12+
parser: 'tsx',
13+
},
14+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = (file, api) => {
2+
const jscodeshift = api.jscodeshift
3+
const root = jscodeshift(file.source)
4+
5+
const replacements = [
6+
{ from: 'react-query', to: '@tanstack/react-query' },
7+
{ from: 'react-query/devtools', to: '@tanstack/react-query-devtools' },
8+
]
9+
10+
replacements.forEach(({ from, to }) => {
11+
root
12+
.find(jscodeshift.ImportDeclaration, {
13+
source: {
14+
value: from,
15+
},
16+
})
17+
.replaceWith(({ node }) => {
18+
node.source.value = to
19+
20+
return node
21+
})
22+
})
23+
24+
return root.toSource({ quote: 'single' })
25+
}

0 commit comments

Comments
 (0)