-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Question: Fetch atoms/selectors via "key"? #181
Comments
If my understanding of the issue is right, this is something you can do with const tempAtom = atomFactory({
key: 'temp',
default: () => 32,
})
function Component() {
const [tempKey, setTempKey] = useState('tempFahrenheit')
const temp = useRecoilValue(tempAtom(tempKey))
// ...
} I've been using a |
@kingdaro - Not sure it's exactly the same thing. I may not have been clear enough. Let me try to expound. Essentially, my primary question is: Why do atom's and selector's have keys that are specified by the developer? From that, two additional thoughts/questions emerged as alluded to in my original post:
^ My hope here is to either get a better understanding of why the key is necessary or to see if this is a planned feature in a future release. Or maybe this already works? Like I said, just trying to better understand the library 👍
|
Hi @soluml , There is some information about the use of keys in #32
|
@acutmore - Thanks for your reply! #32 helps a lot in clarifying things as to why we have keys. Have there been any discussions on making the key optional? As in, have the library generate the string when a unique key is not provided? I guess I personally see myself running into key conflicts (with a team of developers on large projects) more so than needing to persist the unique key of an atom. And also, just to reiterate my first question above. Since we have unique keys, are there any plans to reference atoms/selectors by those keys like in the example above? |
We've discussed this internally. There were some proposals to add Recoil "zones/contexts", which would help solve the key namespace concern and help with composability. Though, this isn't the highest priority at the moment. In the meantime, we use conventions for namespacing keys, e.g. No current plans to allow non-opaque strings as atom/selector handles in the API. Generally we want to enforce that you're actually working with the same shared atom/selector, provide protection from string typos, etc. It has worked pretty well to export shared atoms/selectors from shared modules that can be imported from the components or selectors that need them. |
I'm not sure if it's considered a "best practice", but when implementing |
Don't like fetch by key because could lead to weird behaviour where the |
Yes you can. just warp it with object with key property and the atom key. for example:
The atom itself can be on other file, you dont need to import it, just pass the atom key as a string and it should work (at least for now... 😁). |
According to the docs, each atom/selector need to be given a unique
key
. Is it possible/planned to get an atom/selector by this key? If not, is there a way the library could generate a unique key without forcing the user to provide it?Just trying to get a better understanding of the library. Thanks :)
The text was updated successfully, but these errors were encountered: