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: how to handle get/set for async state

See original GitHub issue

Hi, after watching the awesome video on Recoil, I almost rushed to delete Redux and use Recoil on a large production project with no questions asked. It’s a miracle though that I decided to first try it out.

I’ve seen similar questions in the issues already, but they don’t seem to answer the question - how do you handle setting an async state?. E.g. we have a companies list, and we want to fetch the companies from the API, and then add new company to the list. From what I understand, I could do sth like this:

export const companiesState = atom({
  key: 'companiesState',
  default: [],
});
export const companiesQuery = selector({
  key: 'companiesSelector',
  get: async ({ get }) => {
    get(companiesState);
    const response = await fetch('http://localhost:9000/companies');
    return await response.json();
  },
  set: async ({ set, get }, newCompany) => {
    const response = await fetch('http://localhost:9000/companies', {
      method: 'POST',
      body: JSON.stringify(newCompany),
      headers: {
        'Content-Type': 'application/json',
      },
    });
    const company = await response.json();
    set(companiesState, [company]);
  },
});

Confusing parts for me are:

  • do I need to set the get(companiesState); dependency. All I need is to e.g. push the new company to the list. I could do companiesState || fetch(...) in the getter, but that doesn’t seem to be a good thing(?) as there wouldn’t be an easy way to refresh the state
  • what’s the difference between selector and atom? As far as I can get, the only difference is that selector can be readonly, as atom is always writable. The docs state that selector is a derived state but then I see in the articles and issues selector being used as a state, not always derived from something
  • would you say that in this case, the getting/posting data should be handled in the useEffect in the component instead of the state itself?

Anyway, the library looks very promising - kudos to everyone working on it!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
drarmstrcommented, Sep 10, 2020

@bentron2000 - Here’s the proposed API. We don’t want to expose set during a selector get operation, as that would essentially lead to synchronous side-effects during render.

0reactions
drarmstrcommented, Dec 19, 2020

See these updated guides: async requests and atom effects.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to call an async method from a getter or setter?
An asynchronous method that returns a value. In this case, change the property to an async method. · A value that can be...
Read more >
How to trigger event in the end of async procedure
Hello,. I have an async method. If the method do its work it should trigger an event, but not before.
Read more >
Exploring the async/await State Machine - Vasil Kosturski
In this article, I'll start exploring some of the most popular questions related to async/await. Concretely, in the next couple of posts, ...
Read more >
await - JavaScript - MDN Web Docs
await is usually used to unwrap promises by passing a Promise as the expression . Using await pauses the execution of its surrounding...
Read more >
Awaiting an asynchronous state change - bUnit
You need to handle this specifically in your tests because tests execute in the test framework's synchronization context and the test renderer executes...
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