0

The application is running on page: http://localhost:3000/login and when a button is clicked it should redirect to http://localhost:3000/.

So this is how I've tried:

import React from 'react';
import { Redirect } from 'react-router-dom';
import { Button } from 'semantic-ui-react';

...


<Button onClick={() => <Redirect to="/" />}>
     Go home
</Button>

it doesn't work. What is missing?

Leo Messi
  • 5,157
  • 14
  • 63
  • 125
  • https://stackoverflow.com/questions/1655065/redirecting-to-a-relative-url-in-javascript – Weilory Sep 03 '20 at 14:33
  • 2
    Does this answer your question? [Programmatically navigate using react router](https://stackoverflow.com/questions/31079081/programmatically-navigate-using-react-router) – 0stone0 Sep 03 '20 at 14:34

2 Answers2

0

Since you intend to move out of the app. Why not try

onClick={()=> window.location.replace('/')}

Wakeel
  • 4,272
  • 2
  • 13
  • 14
0

Solution 1: Handling onClick

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

....

const history = useHistory();
<Button onClick={() => {history.push("/");}}>
 Go home
</Button>

Solution 2:

Using button as Link

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

...

<Button as={Link} to="/">
 Go Home
<Button/>

Both of the solution works, however in second case, you might need to handle the styling of the text of Button

Dinesh Singh
  • 727
  • 1
  • 10
  • 22