Redux:维护表单状态的正确做法是什么?
ReactJS + Redux: What's the proper and correct practice to maintain state for a form?
在ReactJS + Redux中,使用Material-UI的TextField
, http://www.material-ui.com/#/components/text-field,我目前有一个表单,用户填写firstName, lastName, birthMonth, birthDay, birthYear。
我有以下内容,它都可以工作,但看起来非常多余,特别是对于出生日期,例如,对于每个输入都有操作,并且每次更改都更新它:
组件InputComponent.js
中:
updateFirstName(event) {
this.props.actions.updateFirstName(event.target.value)
}
updateLastName(event) {
this.props.actions.updateLastName(event.target.value)
}
updateBirthMonth(event) {
this.props.actions.updateBirthMonth(event.target.value)
}
updateBirthDay(event) {
this.props.actions.updateBirthDay(event.target.value)
}
updateBirthYear(event) {
this.props.actions.updateBirthYear(event.target.value)
}
<TextField
hintText="Enter First Name"
onChange={this.updateFirstName}
value={this.props.userInfo.firstName}
/>
<TextField
hintText="Enter Last Name"
onChange={this.updateLastName}
value={this.props.userInfo.lastName}
/>
<TextField
hintText="Enter Birth Month"
onChange={this.updateBirthMonth}
value={this.props.userInfo.birthMonth}
/>
<TextField
hintText="Enter Birth Day"
onChange={this.updateBirthDay}
value={this.props.userInfo.birthDay}
/>
<TextField
hintText="Enter Birth Year"
onChange={this.updateBirthYear}
value={this.props.userInfo.birthYear}
/>
对于我的行为:
updateFirstName(eventValue) {
return {
type: 'UPDATE_FIRST_NAME',
firstName: eventValue
}
},
updateLastName(eventValue) {
return {
type: 'UPDATE_LAST_NAME',
lastName: eventValue
}
},
updateBirthMonth(eventValue) {
return {
type: 'UPDATE_BIRTH_MONTH',
birthMonth: eventValue
}
},
updateBirthDay(eventValue) {
return {
type: 'UPDATE_BIRTH_DAY',
birthDay: eventValue
}
},
updateBirthYear(eventValue) {
return {
type: 'UPDATE_BIRTH_YEAR',
birthYear: eventValue
}
},
然后在我的减速机,userReducer.js
:
const userReducer = function(userInfo = {}, action){
switch(action.type){
case 'UPDATE_FIRST_NAME':
return {
...userInfo,
firstName: action.firstName
}
case 'UPDATE_LAST_NAME':
return {
...userInfo,
lastName: action.lastName
}
case 'UPDATE_BIRTH_MONTH':
return {
...userInfo,
birthMonth: action.birthMonth
}
case 'UPDATE_BIRTH_DAY':
return {
...userInfo,
birthDay: action.birthDay
}
case 'UPDATE_BIRTH_YEAR':
return {
...userInfo,
birthYear: action.birthyear
}
default:
return userInfo
}
}
export default userReducer
对于ReactJS + Redux,是否有更好,更适当和有效的实践来处理输入形式?
提前感谢!
事先警告,我是React的新手,我只是想提供一个思路,太大了,不能作为评论添加。如果这是一个失败,我将删除答案…
可以将所有的更新功能合并到单个updateUserInfo
中吗?
// Component
<TextField name="firstName" onChange={this.updateUserInfo} ... />
<TextField name="lastName" onChange={this.updateUserInfo} ... />
updateUserInfo(event) {
this.props.actions.updateUserInfo(event.target.name, event.target.value);
}
// Action
updateUserInfo(eventProperty, eventValue) {
return {
type: 'UPDATE_USER_INFO',
property: eventProperty,
value: eventValue
};
}
// Reducer
const userReducer = function(userInfo = {}, action){
switch(action.type){
case 'UPDATE_USER_INFO':
var returnObj = {
...userInfo
};
returnObj[action.property] = action.value;
return returnObj;
}
}
备选动作和减速器:
// Action
updateUserInfo(eventProperty, eventValue) {
var updatedData = {};
updatedData[eventProperty] = eventValue;
return {
type: 'UPDATE_USER_INFO',
data: updatedData
};
}
// Reducer
const userReducer = function(userInfo = {}, action){
switch(action.type){
case 'UPDATE_USER_INFO':
return {
...userInfo,
...action.data
};
}
}
我强烈建议您查看redux-form。
你的表单将是这样的(从这里的例子):
import React, {Component} from 'react';
import {reduxForm} from 'redux-form';
class ContactForm extends Component {
render() {
const {fields: {firstName, lastName, email}, handleSubmit} = this.props;
return (
<form onSubmit={handleSubmit}>
<div>
<label>First Name</label>
<input type="text" placeholder="First Name" {...firstName}/>
</div>
<div>
<label>Last Name</label>
<input type="text" placeholder="Last Name" {...lastName}/>
</div>
<div>
<label>Email</label>
<input type="email" placeholder="Email" {...email}/>
</div>
<button type="submit">Submit</button>
</form>
);
}
}
ContactForm = reduxForm({ // <----- THIS IS THE IMPORTANT PART!
form: 'contact', // a unique name for this form
fields: ['firstName', 'lastName', 'email'] // all the fields in your form
})(ContactForm);
export default ContactForm;
相关文章:
- 这是什么 ==- javascript 运算符
- 我的单元测试选项是什么
- 打破承诺链的好方法是什么
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- Javascript中的空白是什么
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- 在JavaScript中拆分日期字符串的更好方法是什么
- 将jQuery.ech()方法转换为本地JavaScript抽象的最佳方法是什么
- 处理浮点错误的最佳方法是什么
- javascript导入的最佳实践是什么
- 基于窗口宽度jquery的函数的替代方法是什么
- 在ng重复循环中显示条件内容的最佳方式是什么
- 我的客户端选项是什么
- 这是什么“;要求“;事情
- 未捕获的类型错误:topFrame.window.changeSelectedBarStyle不是函数,原因是什么
- 实现比较方法的最佳实践是什么;s的比较类型是在运行时选择的
- 从字符串中获取 html 标记的最可维护方法是什么
- Redux:维护表单状态的正确做法是什么?
- 维护服务器端映射代码的正确方法是什么抛出id'S在客户端
- 当点击链接时,维护标签值的干净方法是什么