Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Navigate using react-router

I am developing an application in which I check if the user is not loggedIn. I have to display the login form, else dispatch an action that would change the route and load other component. Here is my code:

render() {     if (isLoggedIn) {         // dispatch an action to change the route     }     // return login component     <Login /> } 

How can I achieve this as I cannot change states inside the render function.

like image 788
Gaurav Mehta Avatar asked May 23 '17 06:05

Gaurav Mehta


People also ask

How do I programmatically navigate with react router?

There are two ways to programmatically navigate with React Router - <Navigate /> and navigate() . You can get access to Navigate by importing it from the react-router-dom package and you can get access to navigate by using the custom useNavigate Hook.

How do you navigate on path by button click in react v6 router?

To redirect page on click of a button with React Router v6, we use the useNavigate hook. to call the useNavigate hook to return the navigate function. Then we call navigate with path to navigate to the path in the routeChange function.

How do I redirect with 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.


1 Answers

Considering you are using react-router v4

Use your component with withRouter and use history.push from props to change the route. You need to make use of withRouter only when your component is not receiving the Router props, this may happen in cases when your component is a nested child of a component rendered by the Router and you haven't passed the Router props to it or when the component is not linked to the Router at all and is rendered as a separate component from the Routes.

import {withRouter} from 'react-router';  class App extends React.Component {      ...      componenDidMount() {         // get isLoggedIn from localStorage or API call         if (isLoggedIn) {              // dispatch an action to change the route              this.props.history.push('/home');         }      }      render() {          // return login component          return <Login />     } }   export default withRouter(App); 

Important Note

If you are using withRouter to prevent updates from being blocked by shouldComponentUpdate, it is important that withRouter wraps the component that implements shouldComponentUpdate. For example, when using Redux:

// This gets around shouldComponentUpdate

withRouter(connect(...)(MyComponent)) 

// This does not

connect(...)(withRouter(MyComponent)) 

or you could use Redirect

import {withRouter} from 'react-router';  class App extends React.Component {      ...      render() {          if(isLoggedIn) {               return <Redirect to="/home"/>          }          // return login component          return <Login />     } } 

With react-router v2 or react-router v3, you can make use of context to dynamically change the route like

class App extends React.Component {      ...      render() {          if (isLoggedIn) {              // dispatch an action to change the route              this.context.router.push('/home');          }          // return login component          return <Login />     } }  App.contextTypes = {     router: React.PropTypes.object.isRequired } export default App; 

or use

import { browserHistory } from 'react-router'; browserHistory.push('/some/path'); 
like image 108
Shubham Khatri Avatar answered Sep 22 '22 12:09

Shubham Khatri