javascript - mapDispatchToProps 函数不起作用 React Redux
问题描述
有带有登录组件的 react-redux。我试图通过 mapDispatchToProps 将 acitons 与组件的道具链接,但得到了错误:
×
TypeError: this.props.login is not a function
这是代码:
import React from 'react'
import {connect} from 'react-redux'
import * as userActions from '../actions/UserActions'
export class Login extends React.Component{
constructor(props) {
super(props)
this.login = this.login.bind(this)
}
login () {
console.log(this)
this.props.login(this.username,this.password)
}
render(){
return <div>
<div class="login-box">
<form>
<div class="form-group">
<label for="exampleInputEmail1">Имя пользователя</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Username" ref = {(input)=>{this.username = input}} />
</div>
<div class="form-group">
<label for="exampleInputPassword1">Пароль</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" ref = {(input)=>{this.password = input}} />
</div>
<button class="btn btn-primary" onClick= {this.login} >Войти </button>
</form>
</div>
</div>
}
}
const mapDispatchToProps = dispatch => {
return {
login:(username,password) => {
dispatch(userActions.login(username,password))
}
}
}
export default connect(null,mapDispatchToProps)(Login)
PS:如果有帮助,我正在使用 react-redux-router
解决方案
推荐阅读
- javascript - 在更大的视图中克隆图片和图像的另一种方法
- swiftui - 令人难以置信的简单代码 (SwiftUI) 不再工作
- python - 如何在 Python 中对运算符及其行为进行分类?
- kotlin - 在 micronaut 属性中使用数据类
- regex - 根据名称更改 pandas 列中的文本
- python - 循环并加载 yaml 文件的压缩文件夹
- google-cloud-platform - 无法在 jupyterlab notebook 谷歌云人工智能平台上运行单元
- excel - 如何抑制 Excel VBA 中的“错误加载扩展”消息?
- javascript - 比较两个街道地址的最佳方法是什么?
- unity3d - unity 将原点移至地图左下角