Answer the question
In order to leave comments, you need to log in
React and asynchronous requests?
I can’t figure out how to correctly implement interaction with the API in React components. Can anyone send code examples or tutorials/manuals?
Answer the question
In order to leave comments, you need to log in
To work with the API, it will be convenient to take a ready-made library whose http request methods return promises or objects with similar properties.
I can advise axios , in a simple case, the native fetch method will suffice.
Then, you can create separate classes - services for working with specific sections of the API (for example, UsersService for working with a list of users, AuthService for authorization). And the service methods take the necessary arguments, and return the axios methods (for example, UsersService.get(id) returns axios(`api/url/users/${id}`))
And you use these services in your "smart" components, in the method componentDidMount.
Link to an article with an example here
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question