react redirect issue I'm getting cannot read setState of undefined

react-router redirect not changing url
react redirect not working
react onclick redirect to external url
react redirect with props
react redirect after login
react redirect onclick
react redirect to another page
react hashrouter

I'm trying to redirect to the second page on button click. Yet, Im getting cannot read setState of undefined when working the code below.

  this.state = {
  arr: [],
  redirect: false
};
setRedirect() {
   this.setState({
  redirect : true
})
}


renderRedirect(){
  if (this.state.redirect) {
  return <Redirect to='/target' />
 }
}

render() {
return (
    <div>
    {this.renderRedirect()}
    <button onClick={this.setRedirect}>Redirect</button>
   </div>
)}

Add setState inside constructor or define as class property

constructor(props) {
  super(props);
  this.state = {
    arr: [],
    redirect: false
  }
}

Or

class abc extends Component {
  state = {
    arr: [],
    redirect: false,
  }
}

Also

Change function to es6 or use bind

ES6 Solution

setRedirect = () => {
   // Code block
}

<Redirect> not working when inside <Switch> · Issue #6840 , This was on a fresh project created with create-react-app, the only thing I added was react-router-dom and then changed the app.js to look like  Redirect Component. If you have routes in your React app, you’re probably using React Router (or React Router DOM) to set up your routes.


Constructor

this.state = {
  arr: [],
  redirect: false
}

function that toggle redirect flag outside constructor

setRedirect=()=>{
  this.setState({redirect:true})
}

finally you render method

render() {
   return (
     <div>
      {this.renderRedirect()}
      <button onClick={this.setRedirect}>Redirect</button>
     </div>
  )
}

I think here is issue of binding this if you haven't written setState in constructor. If so then there are threee ways of changes this so that you setRedirect can have access to this of whole class. (so that it can access state).

  1. binding in render like onClick={()=>this.setRedirect.bind(this)}
  2. binding in constructor like

    this.setRedirect = this.setRedirect.bind(this)

  3. last on considering performance the above menthod setRedirect =()=>{}

Because 1,2 creates new function at every render.

Redirection not work after action · Issue #230 · supasate/connected , I have configurated my app, and router works perfectly (I can use the Redirect component from react-router). The problem that I have detected is  In the below code, we first imported the Redirect component from the react-router-dom library. Redirect component accepts two props from and to. from: we need to type old path here (/blog). to: we need to type a new path here (/tutorials).


You need to bind the setDirect function like this

class App extends React.Component {
 state = {
  arr: [],
  redirect: false
 };
 setRedirect() {
  this.setState({
   redirect: true
  })
 }


 renderRedirect(){
  if (this.state.redirect) {
   return <Redirect to='/target'/>
  }
 }

 render() {
  return (
   <div>
    {this.renderRedirect()}
    <button onClick={this.setRedirect.bind(this)}>Redirect</button> //binding here is important
   </div>
  )
 }
}

or you have to use the constructor of class

constructor(props) {
 super(props);
 this.state = {
  arr: [],
  redirect: false
 }
}

Rendering a <Redirect>, The new location will override the current location in the history stack, like server-​side redirects (HTTP 3xx) do. <Route exact path="/">  import { hashHistory } from 'react-router'; // Triggered somewhere hashHistory.push('/route'); Replace hashHistory with browserHistory if you used that when initializing the Router . This is the method specified at the above link .


You need to move state inside constructor and bind setRedirect in constructor only.

Few Other answers suggested to bind it directly in render don’t do that. Bind it always in constructor

To fix the issue primarily you need to bind setRedirect function

  constructor(props){
        super(props);
        this.state = {
             arr: [],
             redirect: false
         };
         this.setRedirect = this.setRedirect.bind(this);
     }

Also Change

    <button onClick={this.setRedirect}>Redirect</button>

To

    <button onClick={() => this.setRedirect()}>Redirect</button>

Otherwise setRedirect gets called on render so to prevent that use () =>

Redirecting in React - Annee Barrett, Redirect Component. If you have routes in your React app, you're probably using React Router (or React Router DOM) to set up your routes. There seems to be two ways to redirect from an action creator (react-redux): Use withRouter () on component which calls the action creator and pass props.router to the action creator (e.g. dispatch (login (username, password, this.props.router)) and router.push ('/success') inside my action creator) import { browserHistory } from 'react-router' inside the action creator file and browserHistory.push ('/success') as mentioned here: https://github.


React Router Redirect path issue - Web Development, Hi, folks. I'm running into an issue with React Router's Redirect component, and I was wondering if I could get another pair of eyes on it to find  That means no React and no React Router - so the first request will always be to your server. Then, assuming there was a successful GET request, all your JavaScript loads and React Router confidently hijacks your routing. From here on out, any other route changes in your app will be handled by React Router. Notice the issue yet?


React Router Redirect: Programmatic Navigation in React using , I have seen my fair share of react tutorials, but any time that they talk about navigation using reac Tagged with react, javascript, webdev,  I am trying to build a role based access control React app. My vision was that when the App mounts, it checks if user token exists. If it does, it runs a checkAuthToken() which gets the token from local storage and dispatches an action to set the state. Where I am struggling is: redirection doesn't work as I expect it to. Here is my code: in App.js


Redirect on Login and Logout, To ensure that the user is redirected after logging in and logging out of our React.​js app, we are going to use the useHistory React hook from React Router. Modify the server to add the header Access-Control-Allow-Origin: * to enable cross-origin requests from anywhere (or specify a domain instead of *). This should solve your problem. This should solve your problem.