Best way to init a variable depending the data return by an api call
Jehex
1 day ago
Hi there, quick question let's said when the user logs in into my app then it's call an api for get this type ( let said free / premium ) then with this result I init a variable who will display a list of objects into a component ( lets said if the user it's free it's show the list menu for free users and if premium its show the list menu for premium users ) What's can be the easiest / cleanest way to do that knowing that context consumer uses the initial value. I did find a solution but I realize that it's not optimal at all. Thanks
Erik Beuschau
1 day ago
Often it's best not to store API responses in variables, but simply to rely directly on the API's response. If you need to consume this through context, you could create a formula that just returns the API's data.
Hi there, yes im my case I created a formula who return the user type ( free/premium) from the api call then pass it through context to my n-x child component but how use this formula for create my variable ? ( if type_user = free then the variable is init to the free list menu else = to the list premium menu )
Yep this is set by an api call but I would like to know where check if the context is set ( sorry I never do that before )
Lucas G
17 hours ago
On the workflow that is setting the context variable, add a console log there as well
Lucas G
17 hours ago
Then a second one where you are trying to use the context
Jehex
16 hours ago
log 1 show the data ( follower ) second log ( where I can try to get the data return from the context formula ) return undefined ( btw the log 2 showing before the log 1 even with a sleep. dont know if its normal )
Jehex
16 hours ago
I did a test with a big delay ( 5000ms ) and both log showing the data ( so the context is ready )
Jehex
16 hours ago
500ms not working ( context it not ready ) but 1000ms let the time to the component to get the data from the context formula, but 1000 ms :X
Lucas G
16 hours ago
Yeah it’s a slow API. I would recommend setting a check for your contexts instead of a fixed delay
Other option would be to use a faster endpoint to get that initial data
But an init check is still recommended
Jehex
16 hours ago
alright because we talking about a list menu who should show right after the user login so its need to be instant, I need to see what's causing this delay, I dont mind 100-200ms but 1000ms is way to long
Hey Jehex Good question! The best way to handle this is to avoid “hard initializing” a variable from the API call. Instead, you can: 1. Fetch the API response when the user logs in. 2. Store only the raw user type (free/premium) in state or context.
Hello there 😄 thanks for your answer, yep Im using the endpoint auth/me ( who get the user from the auth token right after he logging ) for get the infos needed ( type_user include ). When you said the raw user type in state it's mean only get the type then store it into a variable? Thanks you
Lucas G
14 hours ago
Your issue is just that api is slow
You’re already doing what they’re saying (fetching on load/login) and storing in state (variable exposed via context)