【发布时间】:2020-08-08 01:40:32
【问题描述】:
我是 redux 的新手,我正在尝试通过在我的容器文件中调度 action creator 函数 fetching() 来更新我的应用程序的状态。当我尝试运行我的应用程序时,我收到“无法读取未定义的属性'获取'”错误。这是为什么呢?
//popular reducer
const FETCHING = 'FETCHING'
export function fetching() {
return {
type: FETCHING,
}
}
const initialState = {
isFetching: false,
}
export default function popular(state = initialState, action) {
switch (action.type) {
case FETCHING:
return {
isFetching: true,
}
default:
return state
}
}
//Popular Container
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import * as popularActionCreators from 'redux/popular'
import PropTypes from 'prop-types'
class PopularContainer extends React.Component {
handleFetch() {
this.props.fetching() //Cannot read property 'fetching' of undefined
}
}
PopularContainer.propTypes = {
isFetching: PropTypes.bool.isRequired,
fetching: PropTypes.func.isRequired,
}
function mapStateToProps(state) {
return {
isFetching: state.isFetching
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators(popularActionCreators, dispatch)
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(PopularContainer)
【问题讨论】:
标签: react-redux