在阅读 React/Redux 样板时,我遇到了以下代码片段
import React, { Component } from 'react'
import { connect } from 'react-redux'
import * as actions from '../../actions'
class Signout extends Component {
componentWillMount() {
this.props.signoutUser()
}
render() {
return <div>Bye Bye</div>
}
}
export default connect(null, actions)(Signout)
import axios from 'axios'
import { UNAUTH_USER, AUTH_USER, AUTH_ERROR, FETCH_MESSAGE } from './types'
const ROOT_URL = 'http://localhost:3090'
export function signoutUser() {
localStorage.removeItem('token')
return {
type: UNAUTH_USER
}
}
问题:有人可以解释为什么动作创建者signoutUser()
只需type: UNAUTH_USER
在被调用时返回动作对象,componentWillMount()
并且该动作会神奇地被调度吗?
换句话说,我很困惑为什么没有dispatch
调用,例如
dispatch(this.props.signoutUser())
或者
dispatch({ type: UNAUTH_USER })
如redux 文档中所示。