Skip to content

Commit 79249bb

Browse files
authored
Merge pull request #42 from Grsmto/fix/batch-update-odd-state
Use ReactDOM.unstable_batchedUpdates in the useGraphQL React hook to reduce the number of renders when loading completes, fixing #38 .
2 parents 67c596a + 3f5e57f commit 79249bb

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

.size-limit.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
{
33
"name": "Server",
44
"path": "size-limit-entries/server.mjs",
5-
"limit": "2.5 KB",
5+
"limit": "2.6 KB",
66
"ignore": ["prop-types"]
77
},
88
{
99
"name": "Browser",
1010
"path": "size-limit-entries/browser.mjs",
11-
"limit": "2.5 KB",
11+
"limit": "2.6 KB",
1212
"ignore": ["prop-types"]
1313
}
1414
]

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Updated Node.js support from v8.10+ to v10+.
88
- Updated dependencies, some of which require Node.js v10+.
99
- Replaced the [`tap`](https://npm.im/tap) dev dependency with [`test-director`](https://npm.im/test-director) and [`hard-rejection`](https://npm.im/hard-rejection), and refactored tests accordingly. This improves the dev experience and reduced the dev install size by ~75.5 MB.
10+
- Use `ReactDOM.unstable_batchedUpdates` in the `useGraphQL` React hook to reduce the number of renders when loading completes, fixing [#38](https://github.com/jaydenseric/graphql-react/issues/38) via [#42](https://github.com/jaydenseric/graphql-react/pull/42). Although [`react-dom`](https://npm.im/react-dom) was already a peer dependency, this is the first time it's being used in the client API; potentially a breaking change for atypical projects.
1011

1112
### Patch
1213

src/universal/useGraphQL.mjs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react'
2+
import ReactDOM from 'react-dom'
23
import { FirstRenderDateContext } from './FirstRenderDateContext.mjs'
34
import { GraphQL } from './GraphQL.mjs'
45
import { GraphQLContext } from './GraphQLContext.mjs'
@@ -136,10 +137,11 @@ export const useGraphQL = ({
136137
* @ignore
137138
*/
138139
function onCache({ cacheKey: cachedCacheKey, cacheValue }) {
139-
if (cacheKey === cachedCacheKey && isMountedRef.current) {
140-
setLoading(false)
141-
setCacheValue(cacheValue)
142-
}
140+
if (cacheKey === cachedCacheKey && isMountedRef.current)
141+
ReactDOM.unstable_batchedUpdates(() => {
142+
setLoading(false)
143+
setCacheValue(cacheValue)
144+
})
143145
}
144146

145147
/**

0 commit comments

Comments
 (0)