Redux-form:一次更新/更改两个字段值

时间:2017-08-31 11:08:41

标签: reactjs redux redux-form

我想在Redux-Form中一次更改表单的两个字段值。 存在一种更改方法,但它只允许更改一个字段。 有什么方法可以一次更改多个字段值吗?

实施例。

Fields:
  - patternType
  - color

我想立刻设置两个字段。

1 个答案:

答案 0 :(得分:-1)

不确定是否有更好的方法但是这种方式可行,如果你想根据只有一个的变化两个改变两个输入的值:

const Form = ({change, dispatch}) => {
    const onFirstChange = (event) => {
      const text = event.target.value;
      // make change actions for both inputs
      const changeFirst = change('first', text);
      const changeSecond = change('second', text + text.toUpperCase());
      // dispatch them for both
      dispatch(changeFirst);
      dispatch(changeSecond);
  };
  return (<div>
    First: <Field name='first' component='input' type='text' onChange={onFirstChange} /><br />
    Second: <Field name='second' component='input' type='text' />
  </div>
    )
};

const MyForm = reduxForm({
  form: 'foo'
})(Form);

// END EXAMPLE
ReactDOM.render(
    <Provider store={store}>
    <MyForm />
  </Provider>,
  document.getElementById('root')
);

JSFiddle演示https://jsfiddle.net/jonahe/399cpnz1/1/

相关问题