Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router v4 Redirecting on form submit

Tags:

Brand new to react, Attempting to redirect to a "homepage" after clicking submit on a "login" page. Tried to follow the react router tutorial.

When I click the submit button on the form and console log the state and fakeAuth.isAuthenticated, they both return true. However, the redirect is not firing.

Login.js

class Login extends Component {     constructor(props) {         super(props);         this.state = {             portalId: "",             password: "",             redirectToReferrer: false         }          this.handleSubmit = this.handleSubmit.bind(this);     }       handleSubmit(e) {         fakeAuth.authenticate(() => {             this.setState({                 portalId: this.refs.portalId.value,                 password: this.refs.password.value,                 redirectToReferrer: true             })         })         e.preventDefault();     }       render() {         const redirectToReferrer = this.state.redirectToReferrer;         if (redirectToReferrer === true) {             <Redirect to="/home" />         } 

Routes.js

export const fakeAuth = {     isAuthenticated: false,     authenticate(cb) {         this.isAuthenticated = true         setTimeout(cb, 100)     },     signout(cb) {         this.isAuthenticated = false         setTimeout(cb, 100)     } }  const PrivateRoute = ({ component: Component, ...rest }) => (     <Route {...rest} render={() => (         fakeAuth.isAuthenticated === true         ? <Component {...this.props}/>         : <Redirect to="/" />     )}/>  )   export default () => (     <BrowserRouter>         <div>             <Navbar />             <Switch>                 <Route path="/" exact component={Login} />                 <Route path="/register" exact component={Register} />                 <Route path="/home" exact component={Home} />             </Switch>         </div>     </BrowserRouter> ); 
like image 444
Vincent Nguyen Avatar asked Apr 09 '18 17:04

Vincent Nguyen


People also ask

How do you redirect on Form submit in react?

To redirect on form submit using React Router: Use the useNavigate() hook, e.g. const navigate = useNavigate(); . Call the navigate() function passing it the path - navigate('/contacts') . The navigate() function lets us navigate programmatically.

How do I redirect on a react router?

The Redirect component was usually used in previous versions of the react-router-dom package to quickly do redirects by simply importing the component from react-router-dom and then making use of the component by providing the to prop, passing the page you desire to redirect to.

How do I redirect a URL to another URL in react JS?

Use the window. location. replace() method to redirect to an external url in React, e.g. window. location.


2 Answers

You have to return Redirect in render method (otherwise it will not be rendered and as a result redirection will not happen):

  render() {         const redirectToReferrer = this.state.redirectToReferrer;         if (redirectToReferrer) {             return <Redirect to="/home" />         }         // ... rest of render method code    } 
like image 199
Bartek Fryzowicz Avatar answered Sep 27 '22 20:09

Bartek Fryzowicz


I am now able to redirect to the "homepage" by swapping out <Redirect to="/home" /> with this.props.history.push("/home"); using withRouter. Not sure why the first way would not work though.

like image 36
Vincent Nguyen Avatar answered Sep 27 '22 21:09

Vincent Nguyen