[英]TypeError: dispatch is not a function
我收到此错误,'TypeError: dispatch is not a function' 我做错了什么? 我已经阅读了 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
如果您使用connect
模式,您将能够通过以下this.props.dispatch
获得调度: this.props.dispatch
。
您的inputIsValid
函数试图“解构”第一个参数并从那里获取dispatch
属性。 这是错误的,因为那时您只使用this.state.inputValue
作为参数调用this.inputIsValid
。 但是在任何情况下,您都不应该尝试将dispatch
作为函数中的参数接收。 由于这是一个实例函数(并且您的组件已连接),您可以从this.props.dispatch
访问dispatch
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.