reactjs - 我的函数上没有使用 React 和 Redux 定义调度

标签 reactjs redux redux-thunk redux-middleware

我正在尝试使用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)

最佳答案

一旦您连接您的组件,dispatch 就变成了prop。这同样适用于 xxxxxActions...

在这种情况下,句柄将是:

handleclick(){
  this.props.dispatch(...)
}

关于reactjs - 我的函数上没有使用 React 和 Redux 定义调度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45995002/

相关文章:

reactjs - React-router-dom 和 Redirect 没有被添加到历史记录中?

reactjs - 从另一个 Action 创建者调用一个 Action

javascript - 关于没有中间件的 Redux Async (redux-thunk, redux-saga...)

javascript - 将按钮添加到我的 React Native 应用程序时出现错误?

reactjs - 使用 React、Jest、Redux 和 Thunk 进行无限循环测试

javascript - 使用 React-router 更改组件但不更改主 url

javascript - 如何更改 Yup/Formik 中的默认错误文本?

javascript - 在 Javascript 中迭代分页 API 结果

javascript - npm Bootstrap 不适用于新的 React 项目

javascript - 为什么状态更新后 store.subscribe() 没有触发?