javascript - 类型错误 : dispatch is not a function

标签 javascript reactjs redux

我收到此错误,“TypeError:调度不是函数”我做错了什么?我已阅读 redux 的文档,但我看不出我做错了什么? (redux初学者)故障出在inputIsValid方法

import React from 'react';
import { connect } from 'react-redux';
import { validatedAuthInput } from '../actions/index';

class AuthInput extends React.Component {
    constructor(props) {
        super(props);
        this.state = { inputValue: '' };
    }

    inputIsValid({ dispatch }, value) {
         dispatch(validatedAuthInput(this.props.type, value));

         this.validInput(this.props.type, value);
         return value.includes('@') && value.length > 4 ? 'success' : 'warning';
    }

    handleinput = event => {
        this.setState({ inputValue: event.target.value });
    };

    render() {
        return (
            <FormGroup
                label={this.props.label}
                validationState={this.inputIsValid(this.state.inputValue)}>
                <ControlLabel>{this.props.label}</ControlLabel>
                <FormControl
                    type={this.props.type}
                    value={this.state.value}
                    placeholder={this.props.placeholder}
                    onChange={this.handleinput}
                />
                <FormControl.Feedback />
            </FormGroup>
        );
    }
}

export default connect(null)(AuthInput);

最佳答案

Props 不会自动传递到该函数中,因此您需要在函数内执行 this.props 。我建议您将操作与组件分开,并将 mapDispatchToProps 对象传递给 connect as described here

关于javascript - 类型错误 : dispatch is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48390534/

相关文章:

reactjs - 页面更改时 Redux 状态清除

javascript - React/Redux 无法读取未定义的属性 "currentOpportunities"

javascript - WooCommerce 移动变体描述

javascript - html:文本和createElement

javascript - 如何自动删除所有文件中 native 不需要的导入?

javascript - React 上下文每次使用它时都会返回初始状态 - 尽管 setState

forms - redux-form 字段在表单加载后不会重新渲染/更新

javascript - 基于数组列表显示/隐藏图像

javascript - 使用 Jquery 的 div 中存在的所有控件的 onclick 事件?

javascript - 修改嵌套状态无法更新