简体   繁体   English

React Redux分派不可用

[英]React redux dispatch not available

Originally I had this call at index.js to trigger the load of my main data: 最初,我在index.js进行了此调用以触发主数据的加载:

const store = configureStore();
store.dispatch(doStuff());

Now I want to do the next step and load this data at page level (seems better). 现在,我要进行下一步,并在页面级别加载此数据(似乎更好)。


I'm basing myself on this post by Gaearon at the Redux github : 我将基于Gaearon在Redux github上发表的这篇文章:

在此处输入图片说明


I have this code: 我有以下代码:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { PropTypes } from 'prop-types';
import { bindActionCreators } from 'redux';
import * as myActions from '../../actions/myActions';
import { MuiThemeProvider } from 'material-ui/styles';

let createHandlers = function(dispatch) {
    let doStuff = function() {
      dispatch(myActions.doStuff())
    };

    return {
        doStuff,
      // other handlers
    };
  }


class MyPage extends Component {
    constructor(props, context) {
        super(props, context);

        this.handlers = createHandlers(this.props.dispatch);
        //this.handlers.doStuff();

        this.state = {
            myStuff: []
        }
    }

    render() {
        return (
            <MuiThemeProvider>
                <div>...</div>
            </MuiThemeProvider>
        );
    }
}

function mapStateToProps(state, ownProps) {
    return {
        // Set state
    };
}

function mapDispatchToProps(dispatch) {
    return {
        // Set state
    };
}

MyPage.propTypes = {
        // My props
}

export default connect(mapStateToProps, mapDispatchToProps)(MyPage);

The issue 问题

When I uncomment the line, I get this error: 当我取消注释该行时,出现以下错误:

TypeError: dispatch is not a function TypeError:调度不是函数

let doStuff = function() {  
   dispatch(myActions.doStuff()) 
};

The (most important) difference that I see is that I do mapping: 我看到的(最重要的)区别是进行映射:

export default connect(mapStateToProps, mapDispatchToProps)(MyPage);

What do I need to do to get this to work? 我需要做什么才能使它正常工作?

Likely something easy, but I don't see it. 可能很简单,但我看不到。

Connect's react-redux docs explain that: Connect的react-redux文档说明:

If you do not supply your own mapDispatchToProps function or object full of action creators, the default mapDispatchToProps implementation just injects dispatch into your component's props. 如果您不提供自己的mapDispatchToProps函数或充满动作创建者的对象,则默认mapDispatchToProps实现仅将调度注入到组件的props中。

When you don't pass mapDispatchToProps params to connect , react-redux passes dispatch as a prop to the wrapped component. 当您不将mapDispatchToProps参数传递给connect ,react-redux将dispatch作为道具传递给包装的组件。

If you pass mapDispatchToProps to connect , the wrapped actions are passed instead of dispatch , and this.props.dispatch is undefined. 如果将mapDispatchToProps传递给connect ,则传递包装的动作而不是dispatch ,并且this.props.dispatch是未定义的。

So if you need dispatch in your compon, refrain from using mapDispatchToProps , or wrap all your actions with dispatch inside mapDispatchToProps . 因此,如果您需要在自己的组件中dispatch ,请不要使用mapDispatchToProps ,或者将所有带有dispatch的动作包装在mapDispatchToProps

Oh boy ... I didn't need Gaearon's script at all. 哦,男孩……我根本不需要盖恩的剧本。 All I had to do is call the actions list from the constructor: 我要做的就是从构造函数中调用动作列表:

class MyPage extends Component {
    constructor(props, context) {
        super(props, context);

        props.actions.doStuff();

        this.state = {
            myStuff: []
        }
    }

    render() {
        return (
            <MuiThemeProvider>
                <div>...</div>
            </MuiThemeProvider>
        );
    }
}

Where this is the important line: 这是重要的行:

props.actions.doStuff();

Which is available because it's mapped in mapDispatchToProps : 哪个可用,因为它映射在mapDispatchToProps

function mapDispatchToProps(dispatch) {
    return {
        actions: bindActionCreators(loadOrderActions, dispatch)
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM