20

Ive been trying to do a simple redirect to another component on button click, but for some reason it doesnt work.I want to redirect to '/dashboard' and display AdminServices from login as follows:

//index.js

ReactDOM.render(<BrowserRouter><App /></BrowserRouter>, 
    document.getElementById("root"));

//App.js

     <Switch>
          <Route path="/" component={Login} />
          <Route path="/dashboard" component={AdminServices} />
        </Switch>

//Login.js

<Button
onClick={this.login}
>
</Button>

login = () =>{
    <Redirect to="/dashboard" />
  }

//AdminServices.js

render(){
        return(
            <div>Test</div>
        );
    }

9 Answers 9

32

Instead of using the props.history, a better way is using the useHistory hook like below:

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

const MyComponent = (props) => {
  const history = useHistory();

  handleOnSubmit = () => {
    history.push(`/dashboard`);
  };
};
Sign up to request clarification or add additional context in comments.

2 Comments

is it compatible with React 16.4?
This is outdated, I think @Mohammad Reza 's answer is better
23

In react-router-dom v6 you can use useNavigate(), as answered here.

import { useNavigate } from 'react-router-dom';

const navigate = useNavigate();
navigate('/dashboard');

if you're still using previous versions, you can use useHistory, as Tellisense mentioned.

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

const navigate = useHistory();
navigate.push('/dashboard');

Comments

10

You can route by function by like this

  handleOnSubmit = () => {
  this.props.history.push(`/dashboard`);
  };

Hope this help

2 Comments

Tried already but it still wouldnt show my other component. Also Link gives it a weird underline which ill have to deal with later. How can I just use a function to redirect? :)
I think this should help
1

Simple, use NavLink instead of button

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

<NavLink to="/dashboard"> Dashboard </NavLink>

https://reacttraining.com/react-router/web/api/NavLink

5 Comments

I specifically want to use button as Im using material design and its inside a card. It'll be ideal to use a button in this case..
Sure, I read online that redirecting or linking is a preferred choice than push? Anyways I did try putting NavLink and now it does take me to the right url but for some reason it still shows the same component and not the other one :/
hmmm, not sure, can you try using exact in route i.e. <Route path="/" exact component={Login} />
nvm, I think I just had to use the 'exact' path to get rid of the other component. Works fine now!
1

With react-router-dom v6 you will need to use useNavigate instead of useHistory:

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

export default function MyComponent(){
  const navigate = useNavigate();

  handleOnClick = () => {
    navigate("/dashboard");
  };
};

Comments

0

<Switch> component render only the first route that matches. You just need to swap your <Route> components in <Switch> case and use @Adnan shah answer to make redirect function.

P.S react router auth example

1 Comment

So should I just get rid of the <Switch> at all? You were right about it routing only to the first component.. Now that Im trying to go from '/dashbaord' to '/addservice', it doesnt work :/ Do I remove it completely?
0

This happened to me once I had missed importing the component to the route.js file.

You need to reference/import files from the component to the routefile.

Comments

0

Use an a tag

<a href="/" />

Comments

0

Maybe this might help

import { useHistory } from "react-router-dom";
const history = useHistory();
<Button
onClick={this.login}
>
</Button>

login = () =>{
     history.push("/about");
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.