Javascript Redux表格6.0+;更改所有字段值
我正在尝试以redux形式更改多个值。我把它们放在一个对象中,所以基本上我想用我的对象值覆盖redux表单状态值。一种方法是为每个属性运行Javascript Redux表格6.0+;更改所有字段值,javascript,reactjs,redux,redux-form,Javascript,Reactjs,Redux,Redux Form,我正在尝试以redux形式更改多个值。我把它们放在一个对象中,所以基本上我想用我的对象值覆盖redux表单状态值。一种方法是为每个属性运行this.props.reset(),然后运行多个this.props.change()事件。它可以工作,但它发送的事件太多,速度很慢。我尝试的第二件事是运行this.props.initialize(data,false),这可以工作,但验证不会重新运行,因此我可以轻松地提交表单而无需验证。 有没有办法运行一个事件来用我的对象覆盖表单状态?我担心这是不可能的
this.props.reset()
,然后运行多个this.props.change()
事件。它可以工作,但它发送的事件太多,速度很慢。我尝试的第二件事是运行this.props.initialize(data,false)
,这可以工作,但验证不会重新运行,因此我可以轻松地提交表单而无需验证。
有没有办法运行一个事件来用我的对象覆盖表单状态?我担心这是不可能的。不久前我也遇到了同样的问题,阅读了redux表单中的所有文档后,我得出结论,您必须使用动作创建者。要么更改要么自动填充 如果您使用initialize,那么您正在初始化值,它用于数据的异步初始化,因此,它不会像您所说的那样进行验证 很久以前,在以前的版本中,它们有一个“defaultValue”概念。但是他们把它拿走了。如果你真的不需要上一次更新,也许你应该检查一下这是否对你有帮助 注意
我建议你遵循这一点。他们在那里谈论这件事。这是可能的。我通过createreact应用程序文件结构使用Redux在React中实现了这一点 使用stateProps/dispatchProps模式 您应该已经知道使用此操作的操作和还原程序。 这是我最初开始的项目 我把它包括在内,这样当我使用诸如减缩器和动作之类的术语时,你就会知道我在说什么 在您的操作/索引文件中
import makeAction from "./makeActionCreator";
const clearMultiplePropertiesInState = "clearMultiplePropertiesInState";
export default {
constants: {
clearMultiplePropertiesInState,
},
creators: {
clearMultiplePropertiesInState: makeAction(clearMultiplePropertiesInState
}
};
在您的reducer/{your reducer}.js中
import actions from '../actions';
const { constants } = actions;
const INITIAL_STATE = {
name: "Dr Hibbert",
age: "40",
height: "180cm"
};
//#region const declarations
const { clearMultiplePropertiesInState } = constants;
//#endregion
export default function (state = INITIAL_STATE, action) {
switch (action.type) {
case clearMultiplePropertiesInState: {
var fields = action.data;
var theState = {
...state
};
for(var i = 0; i < fields.length; i++) {
theState[fields[i]] = "";
}
return theState;
}
default:
if (!action.type.includes('@@')) {
console.log(`No action for: ${action.type} type`);
}
return state;
}
}
不,我不想使用旧版本。在您的主题中,我已经问了这个问题,但没有回答。您也可以关注这个问题:抱歉,这不是redux表单解决方案
import React from "react";
import { connect } from "react-redux";
import { Form, Icon, Button, Modal } from "semantic-ui-react";
import actions from "common/actions";
const { creators } = actions;
const MyComponent = ({ stateProps, dispatchProps }) => {
return (
<React.Fragment>
<Button
disabled={disableOkButton}
onClick={() => {
dispatchProps.clearMultiplePropertiesInState(["name", "age", "height"]);
}}
primary
labelPosition='right'
icon='checkmark'
content="Create Club"
loading={stateProps.modalOkButtonLoading}
/>
</React.Fragment>
);
}
function mapStatetoProps(state) {
return {
stateProps: {
name: state.name,
age: state.age,
height: state.height
}
};
}
function mapDispatchToProps(dispatch) {
return {
dispatchProps: {
clearMultiplePropertiesInState: (fieldNames) => {
dispatch(creators.clearMultiplePropertiesInState(fieldNames));
}
}
};
}
export default connect(mapStatetoProps, mapDispatchToProps)(MyComponent);
case addItemToWishList: {
return {
...state,
buyer: {
...state.buyer,
wishlist: action.data
}
};
}