reactjs - Dispatch 不是一个函数 React Redux Typescript
问题描述
在 props 中访问 redux 调度方法时遇到问题。当从 redux-form 调用 handleSubmit 时,dispatch 方法返回“dispatch is not a function”。
我尝试将操作添加到我传递给组件的各种接口中。我传递给 connect 的参数顺序看起来是正确的,以及我用来将操作传递给传递给组件的接口的语法。
PropsFromDispatch 包含 loginRequest 操作,我将其映射到 MapDispatchToProps 方法中的道具。
export interface IAppProps {
loggingIn: any;
}
export interface IAppState {
user: GetTokenRequest;
submitted: boolean;
validate: {
emailState:string
}
}
interface PropsFromDispatch {
loginRequest: (username: string, password:string) => void;
}
type AllProps = IAppProps & PropsFromDispatch & ConnectedReduxProps &
IAppState
export default class LoginPage extends React.Component<AllProps, IAppState,
PropsFromDispatch> {
constructor(props: AllProps) {
super(props);
this.state = this.getInitState();
this.handleUserNameChange = this.handleUserNameChange.bind(this);
this.handlePasswordChange = this.handlePasswordChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
private getInitState(): IAppState {
//Where I'm getting the error
handleSubmit(e: { preventDefault: () => void; }) {
e.preventDefault();
this.setState({ submitted: true });
const { username, password } = this.state;
const {dispatch} = this.props
if (username && password) {
this.props.loginRequest(username, password)
//have also tried
dispatch(loginRequest(username,password))
}
}
render() {
const {loggingIn} = this.props
const {username, password} = this.state;
return (
<Container>
<h3>Sign In</h3>
<Form>
<Col>
<FormGroup>
<Label>Username</Label>
<Input
name="email"
id="exampleEmail"
placeholder="firstname.lastname"
value={ username }
onChange={ (e) => {this.handleUserNameChange(e)} } />
<FormFeedback valid>
Valid Username
</FormFeedback>
<FormFeedback>
Invalid UserName
</FormFeedback>
<FormText>Windows Login</FormText>
</FormGroup>
</Col>
<Col>
<FormGroup>
<Label for="password">Password</Label>
<Input
type="password"
name="password"
id="password"
placeholder="********"
value={ password }
onChange={ (e) => this.handlePasswordChange(e) }
/>
</FormGroup>
</Col>
<Button onClick={(e: any) =>
this.handleSubmit(e)}>Submit</Button>
</Form>
</Container>
);
}
}
function mapStateToProps(state: { authentication: { loggingIn: any; }; }) {
const { loggingIn } = state.authentication;
return {
loggingIn
};
}
const mapDispatchToProps = (dispatch: Dispatch<any>): PropsFromDispatch =>
{
return {
loginRequest: (username: string, password: string) =>
dispatch(loginRequest(username,password))
}
}
const connectedLoginPage = connect(mapStateToProps, mapDispatchToProps)
(LoginPage);
export { connectedLoginPage as LoginPage };
解决方案
推荐阅读
- python-3.x - 如何从数据集中删除图像及其标签
- ruby-on-rails - 在单个 POST HTTP 请求中发送一组不同的哈希值
- java - 返回泛型类型时出现 ClassCastException
- wordpress - Nginx 位置指令中子目录的正则表达式或通配符
- kubernetes - 同一个集群中的不同入口被绑定到不同的地址
- excel - 电子表格::ParseExcel::SaveParser 问题 - 无法获取定义的工作簿对象
- vuejs2 - 从 Nuxt.js 中的注入插件访问其他注入方法
- android - 显示其他类的吐司时内存泄漏
- sql - SELECT 语句包含保留字或参数拼写错误或丢失,或标点符号不正确
- tensorflow - 使用 ssd mobilenet v1 coco 的 output_node_name 进行推理的冻结模型