Skip to content

Commit 83ced96

Browse files
committed
fix(suspense): Suspense instances now unmount properly
1 parent 03614d1 commit 83ced96

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ yarn-error.log*
2626
.history
2727
size-plugin.json
2828
stats.html
29+
.vscode/settings.json

.vscode/settings.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"workbench.colorCustomizations": {
3-
"titleBar.activeBackground": "#ff4154", // change this color!
4-
"titleBar.inactiveBackground": "#ff4154", // change this color!
3+
"titleBar.activeBackground": "#00da63", // change this color!
4+
"titleBar.inactiveBackground": "#00da63", // change this color!
55
"titleBar.activeForeground": "#ffffff", // change this color!
66
"titleBar.inactiveForeground": "#ffffff" // change this color!
77
}

src/queryCache.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ export function makeQueryCache() {
273273
}
274274

275275
query.updateInstance = instance => {
276+
// Filter out any placeholder instances created for suspense
277+
query.instances = query.instances.filter(d => !d.isPlaceholder)
278+
276279
let found = query.instances.find(d => d.id === instance.id)
277280

278281
if (found) {
@@ -291,7 +294,9 @@ export function makeQueryCache() {
291294

292295
// Return the unsubscribe function
293296
return () => {
294-
query.instances = query.instances.filter(d => d.id !== instanceId)
297+
query.instances = query.instances.filter(
298+
d => !d.isPlaceholder && d.id !== instanceId
299+
)
295300

296301
if (!query.instances.length) {
297302
// Cancel any side-effects

src/useBaseQuery.js

+20-9
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,30 @@ export function useBaseQuery(queryKey, queryVariables, queryFn, config = {}) {
6060
[query]
6161
)
6262

63-
// Create or update this instance of the query
64-
query.updateInstance({
65-
id: instanceId,
66-
onStateUpdate: () => rerender({}),
67-
onSuccess: data => getLatestConfig().onSuccess(data),
68-
onError: err => getLatestConfig().onError(err),
69-
onSettled: (data, err) => getLatestConfig().onSettled(data, err),
70-
})
63+
const updateInstance = React.useCallback(
64+
isPlaceholder => {
65+
query.updateInstance({
66+
id: instanceId,
67+
isPlaceholder,
68+
onStateUpdate: () => rerender({}),
69+
onSuccess: data => getLatestConfig().onSuccess(data),
70+
onError: err => getLatestConfig().onError(err),
71+
onSettled: (data, err) => getLatestConfig().onSettled(data, err),
72+
})
73+
},
74+
[getLatestConfig, instanceId, query, rerender]
75+
)
76+
77+
// Create the placeholder instance of this query (suspense needs this to
78+
// to fire things like onSuccess/onError/onSettled)
79+
updateInstance(true)
7180

7281
// After mount, subscribe to the query
7382
React.useEffect(() => {
83+
// Update the instance to the query again, but not as a placeholder
84+
updateInstance()
7485
return query.subscribe(instanceId)
75-
}, [getLatestConfig, instanceId, query, rerender])
86+
}, [getLatestConfig, instanceId, query, rerender, updateInstance])
7687

7788
React.useEffect(() => {
7889
// Perform the initial fetch for this query if necessary

0 commit comments

Comments
 (0)