W
W
Wytpry2018-06-11 20:33:20
React
Wytpry, 2018-06-11 20:33:20

How to implement redirect in react?

There is an authorization page where the user enters a username and password. How can I automatically go to the headpage when I click the Submit button? I'm attaching the part of the code where redirect doesn't work. Is this a bug or wrong approach?

axios.post(apiBaseUrl + "account/signin", payload)
   .then(function (response) {
     console.log(response.data);
     if(response.data.code == 200){
       console.log("Login successfull");
       return (<Redirect to="/Game.js" />);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anamorph, 2018-06-11
@Wytpry

such a redirect will only work in the component,
but it is better to use such an approach as a private routes
wrapper, which every time the transition will check whether the user has the right to view this page, monitoring the token in local storage
More details are described here
https://tylermcginnis.com/react -router-protected-r...

B
Boris Cherepanov, 2019-07-17
@xakplant

Through history.push('/home');
Details in the article "Using browser history and the redirect function ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question