【问题标题】:Redux-form 6.0+ change all field valuesRedux-form 6.0+ 更改所有字段值
【发布时间】:2016-12-16 15:32:32
【问题描述】:

我正在尝试更改 redux-form 中的多个值。我将它们放在一个对象中,所以基本上我想用我的对象值覆盖 redux-form 状态值。实现它的一种方法是运行this.props.reset(),然后为每个属性运行多个this.props.change() 事件。它可以工作,但它发送的事件太多并且速度很慢。我尝试的第二件事是运行this.props.initialize(data,false),这可行,但不会重新运行验证,因此我可以轻松提交表单而无需验证。 有没有办法运行一个事件来用我的对象覆盖表单状态?

【问题讨论】:

    标签: javascript reactjs redux redux-form


    【解决方案1】:

    我害怕这是不可能的。前段时间我遇到了同样的问题,并且阅读了 redux-form 的所有文档,我得出结论,你必须使用动作创建器。要么更改自动填充。

    如果你使用initialize,你是在初始化值,它是用来异步初始化数据的,因此,它不会像你说的那样验证。

    很久以前在以前的版本中,他们有一个“defaultValue”的概念。但他们删除了它。如果您真的不需要进行最后一次更新,也许值得您检查一下这是否会对您有所帮助。

    注意

    我建议你关注这个issue thread。他们在那里谈论它。

    【讨论】:

    【解决方案2】:

    这是可能的。我通过 Create-React-App 文件结构使用 Redux 在 React 中实现了它。

    使用 stateProps/dispatchProps 模式。

    您应该已经了解要使用它的动作和减速器。 这是我最初使用https://medium.com/@notrab/getting-started-with-create-react-app-redux-react-router-redux-thunk-d6a19259f71f开始的项目

    我将其包括在内,因此当我使用减速器和动作等术语时,您会知道我在说什么。

    在你的动作/索引文件中

    import makeAction from "./makeActionCreator";
    
    const clearMultiplePropertiesInState = "clearMultiplePropertiesInState";
    
    export default {
      constants: {
        clearMultiplePropertiesInState,
      },
    
      creators: {
        clearMultiplePropertiesInState: makeAction(clearMultiplePropertiesInState
      }
    };
    

    在你的 reducers/{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;
      }
    }
    

    所以你要清除的三个项目是state.name、state.age和state.height

    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);
    

    正如我所说,您需要精通将 React 与 Redux 结合使用才能理解这一点,但这是可能的。此示例显示我同时重置 3 个值。所以成像也传递新值......

    我通常有一个我使用的 changeSinglePropInState 操作(未包含在代码中),它传递了它想要更改状态的 fieldName 和 fieldValue,因为我不想为我的状态中的每个项目创建一个操作。

    另外,如果你可以绕开它,这会改变状态内对象的一个​​属性

    case addItemToWishList: {
      return {
        ...state,
        buyer: {
          ...state.buyer,
          wishlist: action.data
        }
      };
    }
    

    【讨论】:

    • 对不起,这不是 redux 形式的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 2018-08-31
    相关资源
    最近更新 更多