当前位置:  开发笔记 > 编程语言 > 正文

如何处理Redux的注销路由?

如何解决《如何处理Redux的注销路由?》经验,为你挑选了3个好方法。

我想定义一个可用于注销用户的URL(调度将注销用户的操作).我没有找到显示如何实现路由调度事件的示例.



1> Diego V..:

以下是此类/logout页面的更新实现:

import { Component, PropTypes } from 'react'
import { connect } from 'react-redux'
import { withRouter } from 'react-router'
import * as authActionCreators from '../actions/auth'

class LogoutPage extends Component {

  componentWillMount() {
    this.props.dispatch(authActionCreators.logout())
    this.props.router.replace('/')
  }

  render() {
    return null
  }
}
LogoutPage.propTypes = {
  dispatch: PropTypes.func.isRequired,
  router: PropTypes.object.isRequired
}

export default withRouter(connect()(LogoutPage))



2> Gajus..:

定义路线/authentication/logout:

import React from 'react';
import {
    Route,
    IndexRoute
} from 'react-router';
import {
    HomeView,
    LoginView,
    LogoutView
} from './../views';

export default 
    

    
    
;

创建一个LogoutView调度操作componentWillMount:

import React from 'react';
import {
    authenticationActionCreator
} from './../actionCreators';
import {
    connect
} from 'react-redux';
import {
    pushPath
} from 'redux-simple-router';

let LogoutView;

LogoutView = class extends React.Component {
    componentWillMount () {
        this.props.dispatch(authenticationActionCreator.logout());
        this.props.dispatch(pushPath('/'));
    }

    render () {
        return null;
    }
};

export default connect()(LogoutView);

componentWillMount回调分派两个动作:

破坏用户会话.

将用户重定向到IndexRoute.

this.props.dispatch(authenticationActionCreator.logout());
this.props.dispatch(pushPath('/'));


让事情变得正确.某些组件可能还需要更新并在注销时,但您正在立即触发路由更改.如果你这里没有问题,这只是我个人的观点,可以保留原样.

3> Black-Xstar..:

以下是/logout页面的最新实现:

import React, { Component } from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { Redirect } from "react-router-dom";

import * as authActionCreators from "../actions/auth";

class LogoutPage extends Component {
  static propTypes = {
    dispatch: PropTypes.func.isRequired
  };

  componentWillMount() {
    this.props.dispatch(authActionCreators.logout());
  }

  render() {
    return (
      
    );
  }

}

export default connect()(LogoutPage);

效劳于:

"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-redux": "^5.0.6",
"react-router-dom": "^4.2.2",
"prop-types": "^15.5.10",

推荐阅读
李桂平2402851397
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有