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.

[Question] Is there any way to create a readonly focusAtom? And is it possible to make it work with atomWithImmer?

See original GitHub issue

For example here’s my atoms:

const pagesAtom = atom([{
  children: ['element 1', 'element 2'],
}]);
const currentPageAtom = atom(0);
const currentPageElementsAtom = atom(get => get(pagesAtom)[get(currentPageAtom)].children);
const { index } = props;
const currentElementAtom = focusAtom(currentPageElementsAtom, optic => optic.nth(index)));

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?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Apr 26, 2021

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.

1reaction
dzcpycommented, Apr 26, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Immer — Jotai, primitive and flexible state management for ...
In this bundle, we don't have read-only atoms, because the point of these functions is the immer produce(mutability) function.
Read more >
Remote Programming Setup Using Atom - YouTube
Tutorial of how to setup remote development paradigm using PuTTY and Atom Editor on Windows environment.
Read more >
Atomic Design with React & Typescript: a match made in heaven
Nathalia Rus joined at ReactJS Girls London Meetup to speak about Atomic Design with React & Typescript.
Read more >
Cannot conditionally render nodes under root template - sycamore
Currently, if performing an if/else statement in the root of a template! invocation, an unhandled exception occurs. To Reproduce Steps to reproduce the...
Read more >
Prevent Rerenders and Add Functionality with Jotai Write-only ...
They can receive a write function that will handle the updates to your atom state which will fix the rerender issue. We will...
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