React Redux - this.props.actions.fetchPosts不是一个函数

Tru*_*lv1 2 reactjs react-async react-redux react-thunk

我有从我的组件调用异步操作的问题,我想我做了所有工作所需的一切,但似乎没有,我用过:

mapDispatchToProps

在里面我回来了

actions:bindActionCreators(fetchPosts,dispatch)

我连接它.

在所有这些事情之后,我尝试在我的组件中调用此操作 -

this.props.actions.fetchPosts()

结果我在控制台中收到此错误 -

this.props.actions.fetchPosts不是一个函数

在此输入图像描述

我无法理解它的问题是什么,因为我做了所有事情,这里将是完整的来源:

零件

import React, { Component } from 'react';
import { Link } from 'react-router';
import styles from './Home.css';
import { fetchPosts } from '../actions/counter';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';


    class Home extends Component {

        constructor(props) {
            super(props)
        }
        render() {
                return (
                        <div>
                            <div className="container">
                                <div className="banner_animated">
                                    <p> dadasda</p>
                                </div>
                            </div>
                            <div className="container-fluid">
                                <div className="center">
                                    <input type="text"/>
                                    <button className="btn-2 btn-2a btn" onClick={this.props.actions.fetchPosts()}>Button</button>
                                </div>
                            </div>

                        </div>
                );
        }
}
function mapStateToProps(state) {
        return state
}
function mapDispatchToProps(dispatch) {
    return {
        actions: bindActionCreators(fetchPosts, dispatch)
    };
}

export default connect(mapStateToProps, mapDispatchToProps)(Home);
Run Code Online (Sandbox Code Playgroud)

行动

import { FETCHING_WIN_RATES, FETCHED_WIN_RATES } from '../const';
import { firebaseDb } from './firebase';

const ref = firebaseDb.ref("win_rate");

    function fetchingWinRates() {
        return {
                type: FETCHING_WIN_RATES
        };
}

    function fetchedWinRates(winRates) {
        return {
                type: FETCHED_WIN_RATES,
                winRates
        };
}
// win rate champions
export function fetchPosts() {
        return dispatch => {
                dispatch(fetchingWinRates());
                ref.on("value", function(snapshot) {
                        dispatch(fetchedWinRates(snapshot));
                        console.log(snapshot.val());
                }, function (errorObject) {
                        console.log("The read failed: " + errorObject.code);
                });
        }
}
Run Code Online (Sandbox Code Playgroud)

如果您需要更多文件来帮我写,谢谢.

Cal*_*vin 5

如果将函数传递给bindActionCreators它,它将返回一个函数.请参阅bindActionCreators此处的文档(在" 返回"部分中):http://redux.js.org/docs/api/bindActionCreators.html.

你在this.props.action = fetchPosts这里有效地分配,这意味着你会像这样调用fetchPosts : this.props.action().

如果要访问via this.props.actions.fetchPosts,则需要执行以下操作:

function mapDispatchToProps(dispatch) {
    return {
        actions: bindActionCreators({ fetchPosts }, dispatch)
    };
}
Run Code Online (Sandbox Code Playgroud)

请注意与... { fetchPosts }相同的速记{ fetchPosts: fetchPosts }.