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.

can't update `atomWithDafault` by `set(RESET)`

See original GitHub issue

some useage:

const someAtom = atomWithDafault(async (get) => return fetch('some things'));
// ...
const updateSomeThing useUpdateAtom(someAtom);
const handleUpdate = useCallback(() => {updateSomeThing(RESET)}, [updateSomeThing]);

It’s nothing happend, the async (get) => return fetch('some things') not be invoke, What’s more, image

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
SquirrelJimmycommented, Aug 24, 2021

I would rather recommend to focus on imperative pattern if possible.

The flip side is to focus on declarative pattern.

It’s a bit tricky but you might like it.

const retryAtom = atom(1)
const someBaseAtom = atom(async (get) => {
  get(retryAtom)
  const params = get(someParamsAtom);
  return fetch('some things', params)
})

  // in component
  const retry = useUpdateAtom(retryAtom)
  const handleUpdate = useCallback(() => retry((c) => c + 1), [retry])

I have did this by anothore atom, like :

export function atomWithRefetch<Value>(getDefault: Read<Value>) {
  const overwrittenAtom = atom<Value | null>(null);
  const anAtom: WritableAtom<Value, undefined> = atom(
    (get) => {
      const overwritten = get(overwrittenAtom);
      if (overwritten !== null) {
        return overwritten;
      }
      return getDefault(get);
    },
    (get, set) => {
      const res = getDefault(get);
      if (res instanceof Promise) {
        res.then((v) => {
          set(overwrittenAtom, v);
        });
      } else {
        set(overwrittenAtom, res);
      }
    }
  );
  return anAtom;
}

Thank you, man! atomWithDefault is confusing me.

1reaction
dai-shicommented, Aug 24, 2021

I would rather recommend to focus on imperative pattern if possible.

The flip side is to focus on declarative pattern.

It’s a bit tricky but you might like it.

const retryAtom = atom(1)
const someBaseAtom = atom(async (get) => {
  get(retryAtom)
  const params = get(someParamsAtom);
  return fetch('some things', params)
})

  // in component
  const retry = useUpdateAtom(retryAtom)
  const handleUpdate = useCallback(() => retry((c) => c + 1), [retry])
Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting atom value when resetting selector doesn't work? #773
I want to properly reset it: like I when I would have refreshed the page. So, to cope with that, I built something...
Read more >
atom(options) - Recoil
If a selector is used as the default the atom will dynamically update as the default selector updates. Once the atom is set,...
Read more >
How to reset Atom packages to default - Stack Overflow
If you're on windows, my atom seems to be stored at "C:\Users\{user}\AppData\Local\atom". Here I have access to a folder named "packages".
Read more >
Command line tools | Documentation (Version 2.4) | AtoM
You can do this by changing the value in the app.yml file located in /config/app.yml . The default timeout value in AtoM is...
Read more >
Reset Atom Hub - Vera
Reset WiFi settings will clear corresponding settings on the Hub. Then, after restart, Hub will set up default access point that will make...
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