0

I am new in React js I want to go to home page after successful login react js . Right now I have this function in react and I am using it to go to home.

  handleClick(e) {

    const tok = this.state.username + ':' + this.state.password;
    const hash = Base64.encode(tok);
    const Basic = 'Basic ' + hash;

   
    const history = createHashHistory();

    Axios.get(ServiceName.BaseUrl + '/auth/login', {withCredentials: true, headers: {'Authorization': Basic}})
        .then(res => {

            if (res.status == 200) {
           /*go to page : "/home"*/
            }

        })
        .catch(err => {
            console.log(err);
           
        });

}

or rerdirect

  • Does this answer your question? [How to redirect to another page using history on React js?](https://stackoverflow.com/questions/54243931/how-to-redirect-to-another-page-using-history-on-react-js) – ABGR Jun 29 '20 at 06:13
  • Does this answer your question? [How do I redirect to another webpage?](https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage) – dimiguel Jun 29 '20 at 07:45

2 Answers2

0

You can simply:

history.push('/home')
CevaComic
  • 2,056
  • 2
  • 6
  • 12
0

Try with following way:-

Approach 1:-

    import {Redirect} from 'react-router-dom';

        if (res.status == 200) {
                return  <Redirect  to="/home" />
       }

Approach 2:-

  if (res.status == 200) {
      this.props.history.push('/home');
   }

 
Mehadi Hassan
  • 1,160
  • 1
  • 13
  • 33