close

How to do a redirect to another route with react-router?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to do a redirect to another route with react-router? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to do a redirect to another route with react-router ?

  1. How to do a redirect to another route with react-router?

    to do a redirect to another route with react-router I know this is an old question, but for whoever comes here in 2021 and after as of React Router V6 useHistory is no longer exported from react-router-dom and you have to import useNavigate instead. Sample code is below:

  2. How to do a redirect to another route with react-router ?

    to do a redirect to another route with react-router I know this is an old question, but for whoever comes here in 2021 and after as of React Router V6 useHistory is no longer exported from react-router-dom and you have to import useNavigate instead. Sample code is below:

Method 1

I know this is an old question, but for whoever comes here in 2021 and after as of React Router V6 useHistory is no longer exported from react-router-dom and you have to import useNavigate instead. Sample code is below:

import { useNavigate } from "react-router-dom"

and inside your react class or functional component:

const navigate = useNavigate()
navigate("/404")

Method 2

The simplest solution is:

import { Redirect } from 'react-router';

<Redirect to='/componentURL' />

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment