Redirect to another page in reactjs not working -


i'm trying redirect page in success scenario reactjs project. have tried ways explained in following methods not successful

this.props.router.push('/home');  this.context.router.push('/home');  this.history.pushstate(null, 'home');  browserhistory.push('/home'); 

and many other ways found on stackoverflow. how can this.

my code

function handleloginresponse(response){      switch (response.data.response){          case "200":             /**              * in sccuess senario              * redirect home page              * set cookies token , user id create session              * */              this.history.pushstate(null, 'home');               break;         case "201":             console.log("201");             break;         case "202":             console.log("202");             break;         case "203":             console.log("203");             break;         case "204":             console.log("204");             break;         case "205":             console.log("205");             break;         case "206":             console.log("206");             break;      }  } 

i want redirect page when response of '200'

thank you.

with simple way can use redirect

import { redirect } 'react-router' 

create state connect redirect state

example:

import react, {component} 'react' import { redirect } 'react-router'  export default class myredirect extends component{   constructor(){     super()     this.state = {       redirect : false     }   }    handleloginresponse(){     switch(response){       case '200':         this.setstate({redirect: true})       break;     }   }    render(){     return(       {this.state.redirect && <redirect to="yourpath" />}     )   } } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -