Ris*_*Raj 3 javascript reactjs redux redux-form
SRC /动作/ 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.log("hey");
}
}
Run Code Online (Sandbox Code Playgroud)
SRC /组件/ 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);
Run Code Online (Sandbox Code Playgroud)
我正在使用Redux表格进行签名.这是我点击"登录"按钮时出现的错误.也许signinUser()函数没有正确定义.
bundle.js:29019 Uncaught TypeError:this.props.signinUser不是函数(...)
小智 13
如果有人遇到它(我现在对Udemy做同样的啧啧),这是redux-form 5.x和6之间的一个重大变化.基本上,你只需要通过reduxForm运行它并连接,如图所示此评论https://github.com/erikras/redux-form/issues/1050#issuecomment-221721848.
最终结果如下:
Sigin = reduxForm({ form: 'sigin', fields: [ 'email', 'password' ] })(Sigin);
export default connect(null, actions)(Sigin);
Run Code Online (Sandbox Code Playgroud)
您可以查看我的github repo,了解您在此处使用vx.5.0的redux-form成功实现的相同问题.这是基于Stephen Grider的Advanced React和Redux Udemy课程.如果你喜欢它,那就明星吧.
关于您当前的问题:
Redux Form v6实现了一些不同的逻辑.而不是fieldset你使用Field和导入它与reduxForm一起:
import { reduxForm, Field } from 'redux-form';
Run Code Online (Sandbox Code Playgroud)
由于新版本没有内置的连接方法,您需要:
import { connect } from 'react-redux';
Run Code Online (Sandbox Code Playgroud)
在您的特定实例中,您的其余代码应重构如下:
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;
Run Code Online (Sandbox Code Playgroud)
在Redux表单迁移指南中,您将看到允许您保持更新的正确新语法.
| 归档时间: |
|
| 查看次数: |
3658 次 |
| 最近记录: |