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.
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.
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.
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.
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 byshouldComponentUpdate
, it is important thatwithRouter
wraps the component that implementsshouldComponentUpdate
. 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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With