[英]TypeError: dispatch is not a function
I'm getting this error, 'TypeError: dispatch is not a function' what am I doing wrong?我收到此错误,'TypeError: dispatch is not a function' 我做错了什么? I have read the documentation for redux but I can not see that I'm doing something wrong?
我已经阅读了 redux 的文档,但我看不出我做错了什么? (Beginner at redux) The fault is in the inputIsValid method
(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 is not automatically passed into that function so you need to do this.props
inside the function instead. Props 不会自动传递到该函数中,因此您需要在函数内部执行
this.props
。 I will recommend you separate your actions from your component and pass a mapDispatchToProps
object to connect
as described here我会建议您将您的操作与您的组件分开,并按照此处所述传递一个
mapDispatchToProps
对象以进行connect
如果您使用connect
模式,您将能够通过以下this.props.dispatch
获得调度: this.props.dispatch
。
Your inputIsValid
function is trying to "destructure" the first argument and grab the dispatch
property from there.您的
inputIsValid
函数试图“解构”第一个参数并从那里获取dispatch
属性。 This is wrong because then you're calling this.inputIsValid
with just this.state.inputValue
as an argument.这是错误的,因为那时您只使用
this.state.inputValue
作为参数调用this.inputIsValid
。 But in any case you shouldn't be trying to receive dispatch
as an argument in your function.但是在任何情况下,您都不应该尝试将
dispatch
作为函数中的参数接收。 Since this is an instance function (and you're component is connected), you have access to dispatch
from this.props.dispatch
.由于这是一个实例函数(并且您的组件已连接),您可以从
this.props.dispatch
访问dispatch
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.