Answer the question
In order to leave comments, you need to log in
How to pull api when switching to certain routes to check authorization?
Hello. There is a backend on express and rest api. Authorization by JWT. Studying the react dock and its router, I found an example of how to make a private route, everything is fine, but I'm interested in the possibility of calling a request for a user profile every time I go to routes that require authorization.
To give an analogy, in Vue.js this is done in 2 seconds by using the beforeEach() router hook.
How to do this in react? Thank you.
Answer the question
In order to leave comments, you need to log in
react-router v3 had such an onEnter hook, version 4 does not have it.
In the PrivateRouter component, add a check for new props (using getDerivedStateFromProps or componentWillReceiveProps [deprecated]), where you look - if the route (from match ) has changed - make a request to the API.
If you use a reach router, then you don't know. Didn't watch it.
What is the positioning of the parent?
And best of all the source in the studio.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question