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.

set async API response to atom

See original GitHub issue

I’m new to Recoil. I have an API that gives me user details, something like this

{
"name": userName,
"age" : 28,
"degree": MS,
"address": ...
}

I have a selector that gives me the API response. I would like to store each of these values to a recoil state/atom. So that I can use it across the application. We can’t use an atom setter inside a selector. After getting the selector response from import, I was not able to set in the react component also. as it goes to the infinite loop. Please guide me.

export const userdetails = selector({
    key: 'userdetails',
    get: async ({ get }) => {
        try {
            const response = await axios.get('/user'));
            const data = await response.data;
            return data;
        } catch (error) {
            throw error;
        }
    }
});

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
NethraRaghucommented, Jun 18, 2020

Thanks @dommi10 this helps.

1reaction
dommi10commented, Jun 18, 2020

Yes i think. let’s update the code below:

1. let’s update own selector


export const userDetails = selector({
    key:'userDetails',
    get : async ({ get }) => {
        try{
               const handleUserSearch = get(handleUserSearch);
               let response;
    
               if(handleUserSearch){
                 response = await axios.get('/user'));
                 return response.data;
                }
          return response;
       }catch(error){
         throw error;
        }
    },
    set: async( { set }, newValue)=>{
    //update to server
         try{
             await axios.post('/updateUser',{newValue}));
          }catch(error){
             throw error;
          }
    //update local state
        set(
           handleUserSearch,
           true
        );
    }});

2. to update component value


//imports
import * as React from 'react';
import { useRecoilState } from 'recoil';
import { userDetails } from './store';

//use recoil state to update user
const [ userDetails, setUserDetails ]   = useRecoilState(userDetails);

//create local use state with hooks
const [name, setName ] = React.useState("");

//update function
const updateDetail = () => {
setUserDetails({...userDetails, name});
}

//handle change
const handleNameChange = (e, {value}) =>setName(value);

return(
<>
<input onChange={handleNameChange } placeholder='name'/>
<button onClick={updateDetail}>update</button>
</>
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I set the result of an API call as the default value for a ...
I'm trying to set the default value for an atom in RecoilJS as the value of an asynchronous API call. I'm doing this...
Read more >
React + Recoil - Set atom state after async HTTP GET or ...
This is a quick example of how to set the state of a Recoil atom after sending an async HTTP GET or POST...
Read more >
How to set a new state using async API call #149 - GitHub
The documentation states that to create an async state, the way is creating a selector instead of an atom . The problem is...
Read more >
Asynchronous Data Queries - Recoil
Recoil allows you to seamlessly mix synchronous and asynchronous functions in your data-flow graph of selectors. Simply return a Promise to a value...
Read more >
Async — Jotai, primitive and flexible state management for ...
Async. Using async atoms, you gain access to real-world data while still managing them directly from your atoms and with incredible ease.
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