简体   繁体   English

单击按钮以重定向到父 URL

[英]Button clicked to redirect to parent URL

The application is running on page: http://localhost:3000/login and when a button is clicked it should redirect to http://localhost:3000/ .该应用程序在页面上运行: http://localhost:3000/login并且当单击按钮时,它应该重定向到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?有什么不见了?

Since you intend to move out of the app.由于您打算退出该应用程序。 Why not try为什么不试试

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

Solution 1: Handling onClick解决方案 1:处理 onClick

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

....

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

Solution 2:解决方案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两种解决方案都有效,但是在第二种情况下,您可能需要处理 Button 文本的样式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM