-
It seems like a pretty basic feature/pattern, but I really can't find anything about this online, not even people requesting this feature. Does this pattern fundamentally against Jotai's principles and I'm not seeing it? Anyway, I'm talking about being able to peek into an atom's value without subscribing to changes in that atom. Similar to how Preact Signals do it. For example, you might have an atom holding an access token. When that access token is updated, you don't want to re-evaluate every single atom that uses this access token! You just want the next time an atom is evaluated to read this new token, but not subscribe to the token. const accessTokenAtom = useAtom(
DEFAULT_ACCESS_TOKEN,
(_get, set, newAccessToken) => set(accessTokenAtom, newAccessToken)
);
const shoppingCart = atomWithRefresh((get) => fetch("/cart", {
headers: {
"X-Access-Token": get(accessTokenAtom),
},
}).json()); // let's assume fetch is synchronous for simplicity Now when somebody updates accessTokenAtom, shoppingCart will be re-fetched unnecessarily. I know that the write function (I'm referring to the second argument of |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
Yeah, I think so. It's against declarative data flow model.
Well, then theoretically you don't need to store the token in atoms. // naive code
let accessToken;
const const shoppingCart = atomWithRefresh((get) => fetch("/cart", {
headers: {
"X-Access-Token": get(accessToken),
},
}).json());
What you could do in Jotai is to use store API. |
Beta Was this translation helpful? Give feedback.
-
Usually, a store is created automatically, but you can explicitly create it on your own. import { createStore, Provider, atom, useAtom } from 'jotai'
const countAtom = atom(0)
const myStore = createStore()
const App = () => <Provider store={myStore}><Counter /></Provider>
// You can use it as usual
const Counter = () => {
const [count, setCount] = useAtom(countAtom)
// ...
}
// You can use the store as an escape hatch
console.log(store.get(countAtom)) // This is not reactive Usually, |
Beta Was this translation helpful? Give feedback.
-
I think there is a valid use-case where this is useful: if I want to have the latest value in my callbacks, but don't want to re-render component on every change. const useGetAtomValue = (atom) => {
const store = useStore();
return useCallback(() => store.get(atom)), [store]);
}
const getName = useGetAtomValue(nameAtom);
const doSomeProcessing = useCallback((lastName) => {
return getName() + lastName;
}, [getName]); doSomeProcessing is stable here and getting the value doesn't involve any re-renders. In contrast to const name = useAtomValue(nameAtom);
const doSomeProcessing = useCallback((lastName) => {
return name + lastName;
}, [name]); |
Beta Was this translation helpful? Give feedback.
-
I might be late, but jotai-effect's |
Beta Was this translation helpful? Give feedback.
Usually, a store is created automatically, but you can explicitly create it on your own.