Comments (2)
I just did something similar yesterday, I am implementing a refresh feature and want to trigger a re-render when the refresh is done. Here is the solution I came up with:
// things.hooks.ts
const useRefreshing = () => useApp(R.prop('refreshing'))
export const useMyThings = (): ThinThing[] => {
const fetchThings = useFetchThings()
useRefreshing() // This causes this hook to execute and thereby rerender any time refreshing changes
return suspend(fetchThings, fetchThingsKey)
}
export const useRefreshThings = (): AppState['refreshThings'] =>
useApp(R.prop('refreshThings'))
// things.store.ts
// This is part of a Zustand store, another wonderful pmndrs project :)
export const createThingsSlice: StateSlice<ThingsSlice> = (set, get) => {
...
const clearThings = () => clear(fetchThingsKey)
const refreshThings = async () =>
new Promise<void>(async (res) => {
set({ refreshing: true })
await get().refreshUser()
clearThings()
preload(
() =>
fetchThings().then((things) => {
res()
set({ refreshing: false })
return things
}),
fetchThingsKey
)
})
return {
refreshing: false,
fetchThings,
refreshThings,
clearThings,
}
}
This approach doesn't quite accomplish what I want for refreshing (Suspense briefly renders the fallback), but it works great for re-rendering on clear. You could easily replace the store with useState, I have other reasons for using stores. The refreshThings function is a little ugly because I want to be able to await a call to refreshThings and have it resolve when the new data are available.
from suspend-react.
A quick update... You can avoid the Suspend fallbacks flashing by changing set({ refreshing: false })
to setTimeout(() => {set({ refreshing: false })}, 0)
The flashing of the Suspend fallbacks occurs because calling set({refreshing: false})
before the promise returned by fn
is resolved never gives suspend-react
a chance to cache what the promise resolves to before set({refreshing: false})
queues a re-render with stale data. Then-ing a promise puts a job in the job queue (which is executed immediately) whereas setTimeout
puts a task in the task queue, which is not evaluated until the job queue is empty. So because query
caches the result of the promise returned by fn
in a then
call, if the promise returned by fn
is resolved before suspend
is called again, the fresh data will be immediately available, and suspend
will never throw a promise and causing a suspension. Using setTimeout
to make the call to set({refreshing: false})
a task instead of a job will result in query
finishing its execution before set({refreshing: false})
is called, so when a re-render is triggered, the fresh data have already been cached. At least, that's my best guess as to what's going on!
from suspend-react.
Related Issues (14)
- Cancellation HOT 11
- Contribution Guide HOT 1
- Wrong file ending. HOT 1
- PeerDependencies complaint under React 16.14
- shallowEqualArrays falling short when using empty array as key
- TypeScript Improvements HOT 6
- How to clean ressources ? HOT 7
- Source of 0.0.10 HOT 1
- Assets are not cached correctly using array as useLoader input
- Discussion: Concerns about uncollectable references? HOT 2
- Suggestion: Document ways to avoid key collisions HOT 3
- Fallback to previous render HOT 1
- How would you use suspend-react when fetching through a hook? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from suspend-react.