-
-
Notifications
You must be signed in to change notification settings - Fork 649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] Is there any way to create a readonly focusAtom? And is it possible to make it work with atomWithImmer? #450
Comments
You can do it natively too. // in a component
const { index } = props;
const currentElementAtom = useAtom(useMemo(() => atom((get) => get(currentPageElementsAtom)[index]), [index])); Or, you can try to make // in a component
const { index } = props;
const currentElementAtom = focusAtom(currentPageElementsAtom, useCallback((optic) => optic.nth(index), [index]))); btw, I assume
|
@dai-shi Thanks very much for your quick response. This library is amazing, it's very easy to use and I can see you put a lot of effort in maintaining it. |
So, you solved with the readonly approach? Nice. Closing this as #452 is filed. Thanks for using the lib. Feel free to report more issues. |
@dai-shi Well, I solved the readonly issue but not atomWithImmer one. I tried to dig a bit into it to see if I can create a PR, but it turned out that it's a bit complicated than I thought. Hopefully someone else can work on it to make it more universal. |
Well, the code for #452 would actually be a few lines. (get, set, arg) => {
if (typeof arg === 'function') {
set(anAtom, produce(get(anAtom), arg)
} else {
set(anAtom, arg)
}
} But, types, tests, and docs wouldn't be trivial. |
For example here's my atoms:
However since first parameter should be a WritableAtom so it doesn't work. How about implementing something like focusAtomValue and updateFocusAtom to dress this issue?
Also I found that atoms created by atomWithImmer couldn't be used by focusAtom. I can manually use immer to do the samething in the original atom, but is there a better way to do it?
The text was updated successfully, but these errors were encountered: