Is there an universal way to retrive the value of an atom without using useAtom?
See original GitHub issueIs there an equivalent way of fetching an atom’s value as in zustand
using someStore.getState()
?
I’m currently having some issues to get this to work:
type IdToPathMap = { [key: string]: string }
const idToPathMapAtom = atom<IdToPathMap>({})
type Element = { id: string, payload: any, children: Element[] }
const elementsAtom = atom<Element>([])
export const elementAtomFamily = atomFamily(
(id) => {
// How to get the path here?
const path = idToPathMap[id];
return focusAtom(elementsAtom, (optic) => optic.path(path))
},
isEqual,
);
Or is it better to provide a getter function in the second parameter, so’d be something like this?
return focusAtom(elementsAtom, (optic, get) => optic.path(get(idToPathMapAtom)[id]))
Another use case is that sometimes I need to retrive the data of an atom in an event handler, without causing rerender when the data changes.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Jotai: The Ultimate React State Management
useAtom returns an array of size 2, where the 1st element is a value and the 2nd element is a function, to set...
Read more >A Guide to Jotai: the Minimalist React State Management ...
How to manage global state in React using the minimalist but flexible Jotai library.
Read more >Core — Jotai, primitive and flexible state management ...
Initially, there is no value associated with the atom. Only once the atom is used via useAtom , does the initial value get...
Read more >How To Share State Across React Components with Context
In this tutorial, you'll share state across multiple components using React context. React context is an interface for sharing information ...
Read more >ATOM
Intuitive and easy-to-use, ATOM is more than just an expressive MIDI pad controller: It's a production and performance controller as well.
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 Free
Top 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
Closing as answered. You or anyone can add some more comments, but would be nice to open new issue/discussions for new questions.
With this, it will re-evaluate when
idPathMapAtom
changes and only trigger re-render ifpath
changes.With
getAtom
, it may not trigger re-render whenidPathMapAtom
changes. At least not guaranteed.With this, it will re-render every time
idToPathMap
is changed, regardless of the case ifpath
isn’t changed. So, it’s not something you want.Oh, by the way, we take the memory leak issue seriously. We want to fully support these use cases. Implementation-wise, atom values are stored in WeakMaps, so it should be garbage collected. garbage collection is not free though. If you want more control in caching, you can try
atomFamily
or your own cache.