set async API response to atom
See original GitHub issueI’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:
- Created 3 years ago
- Reactions:1
- Comments:6
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @dommi10 this helps.
Yes i think. let’s update the code below:
1. let’s update own selector
2. to update component value