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.

How to set a new state using async API call

See original GitHub issue

I’m trying to implement basic features to a project using Recoil, just to test the library. I’m really liking what I’m seeing. However, I have a doubt regarding to updating a state based on an async API call, i.e., I created the following selectors to fetch from API:

export const teamsState = selector({
    key: "teams",
    get: async ({ get }) => {
        try {
            const response = await api.get("/teams");
            return response.data.teams;
        } catch (e) {
            throw e;
        }
    },
});

export const teamById = (id) =>
    selector({
        key: "teamById",
        get: ({ get }) => {
            const teams = get(teamsState);

            return teams.filter((team) => team._id === id)[0];
        },
    });

The teamsState fetch a list of teams from the API, and the teamById returns the selected team from the list of teams.

My doubt comes now. If I need to add a new team, how should I proceed?

I have a route to add a team in my backend, and the response is the object team I added, not a list of teams. So, how should I update the teamsState to reflect this added object? Should I get all teams again, making an API call to get the list of teams (this doesn’t seem the right choice, but would work)?

Great work on bringing this new library to the React environment!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

36reactions
amiregyptcommented, May 24, 2020

تزكروني باني احبكم جدا

30reactions
amiregyptcommented, May 24, 2020

هااي

Read more comments on GitHub >

github_iconTop Results From Across the Web

async API calls in useEffects and updating useState
I get the following error: "Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a...
Read more >
Redux Fundamentals, Part 6: Async Logic and Data Fetching
The official Redux Fundamentals tutorial: learn how to use async logic with Redux. ... Make an API call to fetch todos from the...
Read more >
Multiple Ways of Async Await Fetch API Call With ... - YouTube
Lets fetch some data from live api by using fetch with async await in multiple ways. Also we are using hooks (useState and...
Read more >
How To Make API Call In React using useEffect ... - YouTube
How To Make API Call In JavaScript With Fetch API ( Make REST Request ) · Callbacks, Promises, Async Await | JavaScript Fetch...
Read more >
Fetching Asynchronous Data with React Hooks - Giorgio Polvara
Probably the most common use-case for asynchronous code is to fetch a single resource when the component mounts. We need this all the...
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