我想定义一个可用于注销用户的URL(调度将注销用户的操作)。我没有找到显示如何实现路由调度事件的示例。
答案 0 :(得分:10)
以下是此类/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))
答案 1 :(得分:9)
定义路线/authentication/logout
:
import React from 'react';
import {
Route,
IndexRoute
} from 'react-router';
import {
HomeView,
LoginView,
LogoutView
} from './../views';
export default <Route path='/'>
<IndexRoute component={HomeView} />
<Route path='/authentication/logout'component={LogoutView} />
<Route path='/authentication/login' component={LoginView} />
</Route>;
创建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('/'));
答案 2 :(得分:5)
以下是/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 (
<Redirect to="/" />
);
}
}
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",