1

I am trying to create an app with a login functionality. The way I want it to work is that once the login is verified, the program would automatically redirect the user towards the homepage. Right now I have this code:

App.js:

function App() {
  
  return (
    <React.Fragment>
      <div className="content">
       <BrowserRouter> 
        <Routes>
          <Route element={<LoginForm />} path="/login" />
          <Route path="/test" element={<BasicExample />} />
        </Routes>
        </BrowserRouter>
      </div>
    </React.Fragment>
  );
}

export default App;

Login

class LoginForm extends FForm {
  // create a ref object
  state = {
    data: { username: "", password: "" },
    errors: {},
  };

  handleSubmit = event => {
    event.preventDefault();
    const { email, password } = this.state;
    // console.log(this.state);
    console.log('logging')
    axios
      .post(
        "http://127.0.0.1:8000/auth/",
        {
          username: email,
          password: password
        }
        // { withCredentials: true }
      )
      .then(res => {
        console.log('got it')
        console.log(res);
        window.localStorage.setItem("token", res.data.token);
        
        
       
        
        console.log('pushing')
        this.props.history.push('/test', { state: "sample data" })
        // return <Redirect to="/home" />;
      })
      .catch(err => {
        console.log('NOOOOO eror')
        console.log(err);
      });
  } 

  render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          <h1>welcome to log in!</h1>
          <br />
          <input
            type="text"
            name="email"
            placeholder="Username"
            defaultValue={this.state.email}
            onChange={e => {
              this.setState({ email: e.target.value });
            }}
          />
          <br />
          <br />
          <input
            type="password"
            name="password"
            placeholder="Password"
            defaultValue={this.state.password}
            onChange={e => {
              this.setState({ password: e.target.value });
            }}
          />
          <br />
          <br />
          <button type="submit"> Login </button>
        </form>
      </div>
    );
  } 
}

export default LoginForm;

The code outputs TypeError: cannot read properties of undefined (reading 'push'). I assume that the history was not initialized. Where/how should I initialize the history?

2 Answers 2

2

For react-router 6 continue to use element, but use a HOC

Ok, react-router 6 changed the way they do things to only use element, but you have to pass props yourself, and if you're using a class based component then you need to use a HOC)

const LoginComponentWithHistory = (props) => <LoginForm {...props} history={useHistory()}/>

// route

<Route element={<LoginFormWithHistory/>} path="/login" />

See related interesting github convo: https://github.com/remix-run/react-router/issues/8146

For react-router 5

This needs to be passed as component

<Route component={LoginForm} path="/login" />

When you pass it as component, react-router passes in all the props for you (including history)

Sign up to request clarification or add additional context in comments.

7 Comments

I tried passing it as component and got matched leaf root does not have an element
@Razvan updated for react-router 6
version for react-6 works, but now it gives error when trying to execute line this.props.history.push('/test', { state: "sample data" }) from handleSubmit. How can i fix that in v6?
@Razvan what's the error? Same one? Cannot read push of undefined?
Invalid hook call. I made a post about it as well stackoverflow.com/questions/73854940/…
|
0

Try exporting LoginForm with HOC from React Router

export default withRouter(LoginForm);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.