Javascript 未捕获类型错误:this.props.signUser不是函数(…;)
src/actions/index.jsJavascript 未捕获类型错误:this.props.signUser不是函数(…;),javascript,reactjs,redux,redux-form,Javascript,Reactjs,Redux,Redux Form,src/actions/index.js import axios from 'axios'; const ROOT_URL = "http://localhost:3090"; export function signinUser({ email, password }){ return function(dispatch){ axios.post(`${ROOT_URL}/signin`, { email, password }); console.
import axios from 'axios';
const ROOT_URL = "http://localhost:3090";
export function signinUser({ email, password }){
return function(dispatch){
axios.post(`${ROOT_URL}/signin`, { email, password });
console.log("hey");
}
}
src/components/auth
import React, { Component } from 'react';
import { reduxForm } from 'redux-form';
import * as actions from '../../actions';
class Signin extends Component {
handleFormSubmit({ email, password }){
this.props.signinUser({ email, password });
}
render() {
// code
console.log(actions)
const { handleSubmit, fields: { email,password } } = this.props;
return (
<form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
<fieldset className="form-group">
<label>Email:</label>
<input {...email} className="form-control" />
</fieldset>
<fieldset className="form-group">
<label>Password:</label>
<input {...password} className="form-control" />
</fieldset>
<button action="submit" className="btn btn-primary">Sign In</button>
</form>
);
}
// methods
}
export default reduxForm({
form: 'signin',
fields: ['email', 'password']
}, null, actions)(Signin);
import React,{Component}来自'React';
从'redux form'导入{reduxForm};
将*作为操作从“../../actions”导入;
类签名扩展组件{
handleFormSubmit({电子邮件,密码}){
this.props.signUser({email,password});
}
render(){
//代码
console.log(操作)
const{handleSubmit,字段:{email,password}}}=this.props;
返回(
电邮:
密码:
登录
);
}
//方法
}
导出默认reduxForm({
表格:'签名',
字段:[“电子邮件”,“密码”]
},无效,行动)(签名);
我正在使用Redux表单制作登录表单。这是我单击“登录”按钮时出现的错误。可能signinUser()函数的定义不正确
bundle.js:29019未捕获类型错误:this.props.signinUser不是函数(…)尝试使用
actions.signinUser({email,password})
将您的redux表单
依赖项更改为5.3.3版以防有人遇到它(我现在正在Udemy上做相同的tut),这是redux表单5.x和6之间的一个突破性更改。基本上,您只需通过reduxForm和connect运行它,如本文所示
最终的结果是:
Sigin = reduxForm({ form: 'sigin', fields: [ 'email', 'password' ] })(Sigin);
export default connect(null, actions)(Sigin);
您可以查看我的github repo,查看您所面临的问题,这些问题与我使用redux form的v6.5.0成功实现的问题相同。这是基于Stephen Grider的高级React和Redux课程。如果你喜欢它,就给它加星星 关于您当前的问题: Redux Form v6实现了一些不同的逻辑。使用
字段
并将其与reduxForm一起导入,而不是字段集
:
import { reduxForm, Field } from 'redux-form';
由于新版本没有内置的连接方法,您需要:
import { connect } from 'react-redux';
在您的特定实例中,其余代码应按如下方式重构:
const renderInput = field => {
const { input, type } = field;
return (
<div>
<input {...input} type={type} className="form-control" />
</div>
);
}
class Signin extends Component {
handleFormSubmit({ email, password }) {
this.props.signinUser({ email, password });
}
render(){
const { handleSubmit } = this.props;
return (
<form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
<div className="form-group">
<label>Email:</label>
<Field name="email"
type="email" component={renderInput} />
</div>
<div className="form-group">
<label>Password:</label>
<Field name="password"
type="password" component={renderInput} />
</div>
<button action="submit" className="btn btn-primary">Sign in</button>
</form>
);
}
}
function mapStateToProps(state) {
return { form: state.form };
}
Signin = connect(mapStateToProps, actions)(Signin);
Signin = reduxForm({
form: 'signin'
})(Signin);
export default Signin;
const renderInput=field=>{
常量{input,type}=字段;
返回(
);
}
类签名扩展组件{
handleFormSubmit({电子邮件,密码}){
this.props.signUser({email,password});
}
render(){
const{handleSubmit}=this.props;
返回(
电邮:
密码:
登录
);
}
}
函数MapStateTops(状态){
返回{form:state.form};
}
Signin=连接(MapStateTrops,actions)(Signin);
Signin=reduxForm({
表格:'签名'
})(签名);
导出默认签名;
在中,您将看到允许您保持更新的正确的新语法 您是否尝试过调试
this.props
的值?是否确实导入了signInUser函数?