【问题标题】:redux-form fields are resetting when changing a value attached to redux state更改附加到 redux 状态的值时,redux-form 字段正在重置
【发布时间】:2016-07-04 05:16:34
【问题描述】:

我已经配置了一个包含三个字段的 redux-form,如下所示:

function mapState(state) {
    return {
        initialValues: {
            terms: state.register.toJS().terms.value
        }
    };
};

[map dispatch object here]

export const MyFormContainer = reduxForm({
    form: 'myForm',
    fields: ['email', 'password', 'terms']
}, mapState, mapDispatch)(MyForm);

每个字段都根据文档传播传递的道具,例如{...电子邮件}。条款复选框包含一个 onChange 调用 actionCreator 来更新我的 redux 状态:

<input type="checkbox" {...terms} onChange={this.handleTermsToggle.bind(this)}/>

单击复选框时,电子邮件和密码字段都重置为空白,这不是我想要的。

【问题讨论】:

    标签: javascript reactjs redux redux-form


    【解决方案1】:

    原来我从错误的角度解决问题。我需要完全删除状态树的那块,而是在我的外部组件中使用 { change } from 'react-redux' 来修改值。

    import { change } from 'react-redux'
    
    <button onClick={() => {
      this.props.change('myForm', 'terms' true);
    }}>Click Me</button>
    
    ... and don't forget to map change to mapDispatchToProps
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 2018-02-09
      • 2017-12-27
      相关资源
      最近更新 更多