Support useTransition() for showing previous Recoil state with Recoil state changes.
See original GitHub issuei realize that useTransition
is bleeding edge, but i’d like to get an early handle on it. this far i’ve not been able to figure out any combination of parts to get useTransition
to work, and i can’t find any examples online. all my imports work and i’m following the code like this.
recoil: 0.1.1
react: 0.0.0-experimental-4ead6b530
react-dom: 0.0.0-experimental-4ead6b530
and using the concurrent pattern:
ReactDOM.unstable_createRoot(document.getElementById("root")).render(
<BrowserRouter>
<Route path="/" component={App} />
</BrowserRouter>
);
and an otherwise typical and working <Suspense />
pattern:
import React, { Suspense, useState, unstable_useTransition as useTransition } from "react";
import { atom, selector, useRecoilState } from "recoil";
export const baseState = atom({
key: "base",
default: 100,
});
export const resourceState = selector({
key: "resource",
get: ({ get }) => {
const base = get(baseState);
return new Promise(resolve => {
setTimeout(() => {
resolve(base);
}, 2000);
});
},
set: ({ set }, value) => {
set(baseState, value);
},
});
const UseTransitionEample = () => {
const [resource, setResource] = useRecoilState(resourceState);
const [startTransition, isPending] = useTransition({ timeoutMs: 3000 });
return (
<>
<div>resource: {resource}</div>
<div>isPending: {isPending ? "true" : "false"}</div>
<button onClick={() =>
startTransition(() => {
setResource(resource + 1);
})
}>Update</button>
</>
);
}
const SuspenseWrapper = () => (
<Suspense fallback={<div>"Loading..."</div>}>
<UseTransitionEample />
</Suspense>
);
export default SuspenseWrapper;
In the above code clicking the button results in the “Loading…” fallback being presented for the full duration of the “load”.
Issue Analytics
- State:
- Created 3 years ago
- Comments:29 (9 by maintainers)
Top Results From Across the Web
React 18 Transitions - Recoil
React 18 offers a new hook useTransition() for transitioning to a new state while having control over what to render before the new...
Read more >Concurrent UI Patterns (Experimental) - React
When we useTransition , React will let us “stay” on the previous screen — and show a progress indicator there. We call that...
Read more >Recoil updating state when nothing has changed
I am trying to figure out what I need to do in order for it not to do API calls if nothing in...
Read more >recoil | Yarn - Package Manager
Recoil is an experimental state management framework for React. ... with nested renderers that don't support useSyncExternalStore() (#2001, #2010) ...
Read more >which state manager you are using with React 18? : r/reactjs
suspense) is non existant (tearing occurs) and ONLY useTransition() hook from react 18 is currently supported. I have checked mobx state tree ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We’re discussing a possible fix for this now, likely related to a planned optimization as well.
my mistaken understanding as per this description was that the original page content should be displayed only for
timeoutMs
time. hunting around i found this pr which removes that param. so yes i think recoil is doing the right thing now. thanks so much for the hand holding. 😃