Odasaku
Odasaku

Reputation: 367

Redirect not working from react-router-dom

The console log statement is working so I know the function is working except for that there are no errors or any other messages. It is just not redirecting. This is the code:

    const RedirectNow = () => {
      if (registered) {
        console.log("RedirectNow")
        return <Redirect to='/home' />;
      }
    }

The '/home' path is specified in the router in index.js, is that the right way? I also tried a relative path with another file in the same folder to make sure but it did not work. Does someone know what's going wrong? Maybe there's a better way to do this?

Upvotes: 1

Views: 572

Answers (1)

Hafiz Muhammad Bilal
Hafiz Muhammad Bilal

Reputation: 315

You can use useHistory hook for this purpose

import {useHistory} from 'react-router-dom'

and then later:

const history=useHistory()

and as a replacement of redirect statement you can write:

history.push('/home')

Upvotes: 1

Related Questions