question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there an universal way to retrive the value of an atom without using useAtom?

See original GitHub issue

Is 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:closed
  • Created 2 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, May 10, 2021

Closing as answered. You or anyone can add some more comments, but would be nice to open new issue/discussions for new questions.

1reaction
dai-shicommented, Apr 30, 2021

Thanks for your reply.

  const path = useAtom(useMemo(() => atom((get) => get(idToPathMapAtom)[id])), [id]))

In this case let’s say idToPathMapAtom got updated, since it’s cached based on id, will it trigger an update? If not path would be stale then.

With this, it will re-evaluate when idPathMapAtom changes and only trigger re-render if path changes.

With getAtom, it may not trigger re-render when idPathMapAtom changes. At least not guaranteed.

Or should it be more like this?

  const idToPathMap = useAtomValue(idToPathMapAtom)
  const path = useAtom(useMemo(() => atom((get) => idToPathMapAtom[id])), [id, idToPathMap]))

With this, it will re-render every time idToPathMap is changed, regardless of the case if path isn’t changed. So, it’s not something you want.

But with this approach, when idToPathMap changes very fraquently, the cache might keep growing, it won’t cause a huge problem I guess, but it could cause small memory leaks. I’d still prefer there is a better approach, if there is any

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found