-
Hi Jotai community, I'm new to Jotai but so far think it's pretty great! My question: I have a component which knows how to handle Suspense and uses This isn't my component, but just as an example:
Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
There's no straightforward way, but you can use the store api: // in component
const store = useStore();
const [promise, setPromise] = useState(() => store.get(asyncAtom));
useEffect(() => store.sub(asyncAtom, () => setPromise(store.get(asyncAtom))), [store, asyncAtom]); |
Beta Was this translation helpful? Give feedback.
-
Hi, I am using this solution for now. import { Atom } from 'jotai';
import { useStore } from 'jotai/react';
import { useRef } from 'react';
export const useSyncAtom = <Value>($atom: Atom<Value>) => {
const store = useStore();
const atomRef = useRef<Value>();
if (atomRef.current === undefined) {
atomRef.current = store.get($atom);
store.sub($atom, () => {
atomRef.current = store.get($atom);
});
}
return atomRef.current;
}; |
Beta Was this translation helpful? Give feedback.
When we designed the hook api, and even for now,
use
isn't officially available. So, such usage is out of the scope. The canonical usage is to useuseAtom
where you want to consume an atom and suspend.Alternatively, you can store a promise in an object (or an array) as an atom value:
fwiw, Valtio v2 API will be designed with
use
in mind. pmndrs/valtio#810