Dynamically set values of an atomFamily
See original GitHub issueThere 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:
- Created 2 years ago
- Comments:13 (7 by maintainers)
Top 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 >
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
You need to track the params to use the family on your end.
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.Yes, please open a new discussion.