React router v4 使用声明式重定向而不渲染当前组件

IT技术 javascript reactjs react-router-v4
2021-05-17 02:15:08

我使用的是类似的代码像这样在我的应用程序将用户重定向登录后的代码如下所示:

import React, { Component } from 'react'
import { Redirect } from 'react-router'

export default class LoginForm extends Component {
  constructor () {
    super();
    this.state = {
      fireRedirect: false
    }
  }

  submitForm = (e) => {
    e.preventDefault()
    //if login success
    this.setState({ fireRedirect: true })
  }

  render () {
    const { from } = this.props.location.state || '/'
    const { fireRedirect } = this.state

    return (
      <div>
        <form onSubmit={this.submitForm}>
          <button type="submit">Submit</button>
        </form>
        {fireRedirect && (
          <Redirect to={from || '/home'}/>
        )}
      </div>
    )

  }
}

当成功登录被触发时工作正常。但有一种情况,登录用户进入登录页面并应自动重定向到“主页”页面(或任何其他页面)。

如何在不呈现当前组件并且没有(据我所知不鼓励)强制推送到历史记录(例如 in componentWillMount)的情况下使用重定向组件

2个回答

方案一

您可以使用withRouterHOC 通过 props 访问历史记录。

用路由器导入。

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

然后用 HOC 包裹。

// Example code
export default withRouter(connect(...))(Component)

现在您可以访问this.props.history. 例如,将它与componentDidMount().

componentDidMount() {
  const { history } = this.props;

  if (this.props.authenticated) {
    history.push('/private-route');
  }
}

解决方案 2好多了

这是关于reacttraining 的示例

这对你来说非常有用。

但是您只需要创建LoginRoute来处理您描述的问题。

const LoginRoute = ({ component: Component, ...rest }) => (
  <Route
    {...rest} render={props => (
    fakeAuth.isAuthenticated ? (
        <Redirect to={{
          pathname: '/private-route',
          state: { from: props.location }
        }} />
      ) : (
        <Component {...props} />
      )
  )} />
);

里面<Router />只是更换

<Route path="/login" component={Login}/>

<LoginRoute path="/login" component={Login}/>

现在,每次有人尝试以经过/login身份验证的用户身份访问路由时,他都会被重定向到/private-route. 这是更好的解决方案,因为它不会安装您的LoginComponentif 条件。

这是另一个完全不涉及 React 内容的解决方案例如,如果您需要在 redux-saga 中导航。

有文件history.js

import {createBrowserHistory} from 'history';
export default createBrowserHistory();

在你定义路由的地方,不要使用浏览器路由器,而只是通用<Router/>

import history from 'utils/history';

...

<Router history={history}>
  <Route path="/" component={App}/>
</Router>

而已。现在您可以使用相同的历史导入并推送新路线。

您的应用程序的任何部分

import history from 'utils/history';
history.push('/foo');

传奇中

import {call} from 'redux-saga/effects';
import history from 'utils/history';

 ... 

history.push('/foo');
yield call(history.push, '/foo');