Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this.props.history.push is undefined

I am creating a form with some predefined values, and i want to route to the dashboard page once the form is submitted. I am using handleLoginSubmit func on the onsubmit of the form. For that, I have the following code:

handleLoginSubmit = (e) => {
e.preventDefault();
let hardcodedCred = {
  email: "[email protected]",
  password: "password123",
};

if (
  this.state.email == hardcodedCred.email &&
  this.state.password == hardcodedCred.password
) {
  //combination is good. Log them in.
  //this token can be anything. You can use random.org to generate a random string;
  // const token = "123456abcdef";
  // sessionStorage.setItem("auth-token", token);
  //go to www.website.com/todo
  // history.push("/dashboard");
  this.props.history.push("/dashboard");

  // console.log(this.state.route);
  console.log(this.context);
  console.log("logged in");

  // <Link to={location} />;
} else {
  //bad combination
  alert("wrong email or password combination");
}


};

But, I am receiving the error saying that history is undefined. Please help me out here

like image 787
Adeena Lathiya Avatar asked May 17 '26 19:05

Adeena Lathiya


1 Answers

You need to export the component with router to access history

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

export default withRouter(ComponentName)
like image 129
Sinan Yaman Avatar answered May 20 '26 12:05

Sinan Yaman