【发布时间】:2020-03-11 09:33:32
【问题描述】:
谁能告诉我重定向在反应中是如何工作的?
我来自 Angular 背景,正在尝试学习 React。
我有一个小型应用程序,我想通过单击按钮将用户重定向到 profile 组件。
这是一个非常基本的要求,但没有适当的文档可用(或者我找不到)。
任何帮助将不胜感激。
这是我的代码:
import { BrowserRouter, Route, Switch, Redirect } from "react-router-dom";
<BrowserRouter>
<Switch>
<Route path="/" exact render={props => <Index {...props} />} />
<Route path="/login-page" exact render={props => <Login {...props} />} />
<Route path="/profile-page" exact render={props => <Profile {...props} />} />
<Route path="/dashboard" exact render={props => <Dashboard {...props} />} />
<Redirect to="/" />
</Switch>
</BrowserRouter>,
<Button color='primary' onClick={e => this.viewProfile()}>View Profile</Button>
viewProfile = function () {
console.log("clicked");
}
【问题讨论】:
-
是的,我看到了这个解决方案,但是返回的是什么
;方法?由于未定义“重定向”,因此出错。 -
import { Redirect } from 'react-router' 从 react-router 导入它