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.

Dynamically set values of an atomFamily

See original GitHub issue

There is probably a different/better ways to achieve this but I wanted to model a selection dialogue using atomFamily booleans:

const selectedLanguages = atomFamily<string, boolean>(() => false)

Now, the selected languages are returned from an API which I want to keep consistent.

I’m imagine an atom that lets me pass in the id dynamically. So instead of having just a value, get something that exposes the method that looks up the value via id.

const Component = () => {
  allLanguages = ["de", "fr", "en"]
  [_selectedLanguages, setSelectedLanguages] = useAtomFamily(selectedLanguagesState) <= doesn't exist

  useEffect(() => {
    const selectedLanguagesFromApi = fetchLanguages()

    allLanguages.forEach((lang) => {
      if(selectedLanguagesFromApi.include(lang)) {
        setSelectedLanguages(lang, true)
      }
    })

  }, [])


  return(<Something />)
}

Am I thinking about this wrong?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
dai-shicommented, Mar 28, 2021

You need to track the params to use the family on your end.

const selectedLanguages = atomFamily<string, boolean>(() => false)

const availableLanguages = atom<string[]>([])

const selectedLanguagesState = atom(
  (get) => get(availableLanguages).filter((lang) => get(selectedLanguages(lang))),
  (get, set, { lang, value }) => {
    if (!get(availableLanguages).includes(lang)) {
      set(availableLanguages, [...get(availableLanguages), lang])
    }
    set(selectedLanguages(lang), value)
  }
)

const Component = () => {
  allLanguages = ["de", "fr", "en"]
  [_selectedLanguages, setSelectedLanguages] = useAtom(selectedLanguagesState)

  useEffect(() => {
    const selectedLanguagesFromApi = fetchLanguages()

    allLanguages.forEach((lang) => {
      if(selectedLanguagesFromApi.include(lang)) {
        setSelectedLanguages({ lang, value: true })
      }
    })

  }, [])


  return(<Something />)
}

It might be possible to consider adding selectedLanguages.params() feature. But, it’s not reactive, so I don’t think it’s useful in this use case.

1reaction
dai-shicommented, Aug 29, 2022

Also, if this would be better in a new issue, please move it / let me know.

Yes, please open a new discussion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to dynamically assign data to an atomFamily with Recoil?
So it seems like I should have const setterFactory = someKey => useSetRecoilState(useAtomFamily(someKey)) but that violates the rule of hooks.
Read more >
atomFamily(options) - Recoil
A primitive value; An array, object, Map , or Set of serializable values; Contain a toJSON() method which returns a serializable value ......
Read more >
[Solved]-Dynamic atom keys in Recoil-Reactjs - appsloveworld
Coding example for the question Dynamic atom keys in Recoil-Reactjs. ... <h1>Custom defaults using atomFamily</h1> {fields.map(({id, value: defaultValue}) ...
Read more >
Jotai vs. Recoil: What are the differences? - LogRocket Blog
In Recoil, the initial value is set in the default option and could ... Jotai are unified under the atomFamily (exported by jotai/utils...
Read more >
Recoil - Ideal React State Management Library?
Provider> gets a new value, all the components that consume that value ... dynamic global states, by using atomFamily and selectorFamily.
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