E
E
Egor Dovydenko2021-10-21 13:57:06
React
Egor Dovydenko, 2021-10-21 13:57:06

How to track page reload in React Router for api requests?

A simple example:
There are a couple of buttons, by clicking on which an api request is made.
Information is displayed by one component by path='/wether/:city'

61714625c198d270083908.png

When reloading the page, there is no information at the required url - because the api request naturally did not occur.
So the question is - how to make sure that the necessary information is displayed when the page is reloaded.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2021-10-21
@Doktorjd

how to make it so that when the page is reloaded, the necessary information is displayed
Detach loading from buttons and attach to the life cycle of the component (or hooks). Buttons should only change the URL, the router, in response to the change, renders the component and requests data in it. Thus, when the router renders the component in the same way when opening the page via a direct link, the request will be made automatically.
There are quite a few examples of specific implementations, choose any.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question