-
When using an atom that wraps a promise along with suspense, Is is possible to trigger suspension when props passed to the atom as args change? Or is this a case where I saw a relevant comment here React transitions don't apply to suspense. But if they ever do, this will be cool which seems to imply that I should use my own loading code making use of PromiseState when I care about subsequent re-loads showing the loader.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
Hey @joprice. Components using any atom hooks will re-suspend when the atom's const suspendingAtom = atom('suspending', () => {
// quick example that changes the promise reference every time this atom reevaluates:
return api().setPromise(new Promise(resolve => setTimeout(() => resolve('test'), 1000))))
})
function ExampleComponent() {
const suspendingInstance = useAtomInstance(suspendingAtom)
return <button onClick={() => suspendingInstance.invalidate()}>Re-suspend</button>
}
function ExampleSuspenseWrapper() {
return (
<Suspense fallback={<div>ExampleComponent suspending...</div>}>
<ExampleComponent />
<Suspense>
)
} Note that you can use
This should also "just work" to re-suspend the component, but in this case, it'll be because the different params create an entirely new atom instance. function ExampleComponent() {
const [counter, setCounter] = useState(0)
// creates a new atom instance every time `counter` changes.
const suspendingInstance = useAtomInstance(suspendingAtom, [counter])
return <button onClick={() => setCounter(state => state + 1)}>Re-suspend</button>
} (an example like this should be paired with Transitions are completely different from suspense. It doesn't sound like you're referring to transitions (via |
Beta Was this translation helpful? Give feedback.
Hey @joprice. Components using any atom hooks will re-suspend when the atom's
.promise
reference changes.Note that you …