Skip to content
Advertisement

React Native UseEffect API CALLS

How to handle multiple api calls in the same render ?

Example :

I want to get some info from first API call like this for example :

JavaScript

and i want to instantly use it for the next API CALL coming under this call

example :

JavaScript

Soo the question is how to use information that i get inside first API call just after that in the api call below. because wihtout that information for example user.id_picture my second api call will return 500.

Thanks for the help 🙂

Advertisement

Answer

Firstly, I would create a couple of functions like so:

JavaScript

I would also introduce 2 state variables, you may already have these so this step may be unnecessary.

JavaScript

Fill out the logic for your functions you added above.

JavaScript

Next, create two useEffects:

  • When the component mounts, call fetchUserInfo.
  • When the avatarId has been retrieved and ultimately set in state, call fetchActiveProfilePicture.
JavaScript

You’ll run into some warnings from eslint (react-hooks/exhaustive-deps) within this example about either wrapping functions in useCallback or placing the logic directly in useEffect. Just a heads up.

Here’s an example on CodeSandbox with the PokeAPI.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement