Make API calls in React.
May 31, 2021
The traditional way of calling API asynchronously is creating three local States and changing their states manually.
Example:-
And changing their state in the function where you call the API-
this makes your code lengthy and somewhat not easy to understand,
as when you feel you should split your code into multiple files, you should split it.
Now we are going to do the same thing but will be creating our custom hook to call API, that hook can be used at multiple places-
- Create a new file (you can name anything relevant )- UseApi.js
2. calling the above custom hook in the main file.
you can use this constant to conditionally render your code