【发布时间】:2018-02-10 05:01:58
【问题描述】:
我正在尝试使用react-redux-loading-bar 在从 API 服务器获取数据期间显示加载栏,我不使用 promise 中间件,所以我决定不使用它,示例说明这样做
import { showLoading, hideLoading } from 'react-redux-loading-bar'
dispatch(showLoading())
// do long running stuff
dispatch(hideLoading())
它给了我这个。
Uncaught ReferenceError: dispatch is not defined
我与其他库有类似的问题并放弃了那个时间,这次我想真正了解它是如何工作的,因此非常感谢任何信息。下面是导致错误的代码,删除了特定的函数和类名。
import React from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import { showLoading, hideLoading } from 'react-redux-loading-bar'
import * as xxxxxActions from '../../actions/xxxxx'
class xxxxxx extends React.Component {
constructor(props) {
super(props)
this.handleclick = this.handleclick.bind(this)
}
handleclick(){
dispatch(showLoading())
asynchronousGetFunction( target_url, function (data) {
dispatch(hideLoading())
})
}
render() {
return <li onClick={this.handleclick}>yyyyyyy</li>
}
}
function mapStateToProps( state ){
return {
}
}
function mapDispatchToProps(dispatch, state) {
return {
xxxxxActions: bindActionCreators( xxxxxActions, dispatch )
};
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(xxxxxx)
【问题讨论】:
-
当您使用 bindActionCreators 时,每个动作创建者都被包装到一个调度调用中,因此可以使用道具直接调用它们。因此,在您的情况下,要调度,请使用类似 this.props.xxxxxActions.yourActionToDispatch()
标签: reactjs redux redux-thunk redux-middleware