Calling a function in React: Uncaught TypeError: ...is not a function(…)

Calling a function in React: Uncaught TypeError: ...is not a function(…)

我正在尝试添加一个用户,目前我只是在尝试让调用正常工作,因此到目前为止还没有数据被传递。我的容器看起来像这样:

用户-add.js

import React, {Component} from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {createUser} from '../actions/index'

class UserCreate extends Component {
    render() {
        return (
            <div>
                <h2> Add User </h2>

                <table className="userTable">
                <tbody>
                <tr>
                    <td>First Name:</td>
                    <td>Last Name:</td>
                </tr>

                <tr>
                    <td>
                        <input type="text"
                            placeholder="Hello!"
                            value="Val" />
                    </td>
                    <td>
                        <input type="text"
                            placeholder="Hello!"
                            value="Val" />
                    </td>
                </tr>
                </tbody>
                </table>


                <button onClick={() =>this.props.createUser()}>Submit</button>

            </div>
        );
    }
}

function mapStateToProps(state) {
    return {
        user: state.activeUser
    };
}

function matchDispatchToProps(dispatch){
    return bindActionCreators({createUser: createUser}, dispatch);
}

export default connect(mapStateToProps)(UserCreate);

在我的用户的 reducer 中,我已经将相应的 case 添加到我的 switch 语句中:

switch (action.type) {
    case 'USER_DELETED':
        return state.filter(user => user.id !== action.userIdToDelete);
    case 'USER_CREATED':
        return state;
}

当然我也像这样将动作添加到我的 actioncreators

export const createUser = () => {
    console.log("You created user: XX ");
    return {
        type: 'USER_CREATED',
        payload: {}
    }
};

然而,它给了我:

Uncaught TypeError: _this2.props.createUser is not a function(…)

有什么想法吗?我对此很陌生,已经 post 在这里编辑过几次(不过还有其他问题),但我不是很明白 wrong.If 你需要任何其他代码片段,让我知道,我' ll post 他们。我知道代码 posted 不完整,请记住我只是想要按钮调用函数等,实际创建用户是另一个问题。

您没有在任何地方使用 matchDispatchToProps(应该是 mapDispatchToProps?)。

您需要将其添加到您的连接中:

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