我想定义一个可用于注销用户的URL(调度将注销用户的操作).我没有找到显示如何实现路由调度事件的示例.
以下是此类/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))
定义路线/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('/'));
以下是/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",