How do i make a react app redirect to the login page after registration?

Viewed 321

I am doing some bugfixing for another developers React app and I have run into something I dont fully understand. When a user registers on the application, it should redirect them to the Login page, but instead it is showing a text box saying "No Content" and not redirecting anywhere.

if (res.status == 204 || res.statusText == "No Content") {
    setSessionCookie("")
    window.location.reload(); }

From what i can tell this small snippet seems to be what should redirect but it seems to only be reloading the same page? I dont know if I am looking in the complete wrong place or not. Help please

2 Answers

Use react-router-dom

You can use react-router-dom (npm package)

In React Class you can use this: this.props.history.push('/login')

And in React Hooks you can use useHistory from react-router-dom:

import { useHistory } from "react-router-dom";

export default function SomeFunction() {

  const history = useHistory();

  const trigerFunction = () => history.push('/login');

  return (
     <div onClick={trigerFunction}></div>
  )
}

BTW

window.location.reload(); it's only refresh the page.

You can save in local storage some that after refresh you know what page to render (but I don't recomend this, and the refresh is unnecessary)

You can change:

window.location.reload();

this.props.history.push("desired Address");

Related