Skip to content

How to 'get' another atom's value in atomWithStorage storage-functions? #2684

Answered by dai-shi
PhilipGrefe asked this question in Q&A
Discussion options

You must be logged in to vote

If atomFamily works for your case, it might be easier.

atom-in-atom is the way to create an atom which internally creates atomWithStorage.

const myAtomInAtom = atom((get) => atomWithStorage(
  'foo',
  'bar',
  {
    getItem: async (key, initialValue) => {
      const value = get(anotherAtom) 🎉 
      // query storage using 'value'
      return "baz"
    },
    //...
))

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@PhilipGrefe
Comment options

@dai-shi
Comment options

@PhilipGrefe
Comment options

@dai-shi
Comment options

Answer selected by PhilipGrefe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants