如何用reduxform计算?

时间:2019-04-16 12:15:59

标签: reactjs redux-form

我是redux形式的新手,尝试添加两个值,两个看起来像这样的字段:

char*

我使用在handleChange事件中调用的函数:

 <Field
          className="uk-input"
          name="amount1"
          component="input"
          type="text"
          placeholder="dollars"
          onChange={this.handleChange}
        />

如何计算总数并将其存储在redux-form存储中?

1 个答案:

答案 0 :(得分:1)

您可以使用componentDidUpdate(并删除handleChange)来计算更新后的总和:

componentDidUpdate() {
    const { amount1, amount2 } = this.props;
    const tootal = parseFloat(amount1 || 0) + parseFloat(amount2 || 0);
    this.props.change("selectingFormValues", "totaal", tootal);
  }

并将总成分更改为input

<Field className="uk-select" name="totaal" component="input" />

查看更新后的示例https://codesandbox.io/s/jl6pwj2r75

或者,您可以在选择器中而不是在componentDidUpdate中进行计算:

componentDidUpdate() {
    this.props.change("selectingFormValues", "totaal", this.props.tootal);
}

并在选择器中:

const mapStateToProps = state => {
  const { amount1, amount2 } = selector(state, "amount1", "amount2");
  const tootal = parseFloat(amount1 || 0) + parseFloat(amount2 || 0);
  return {
    amount1,
    amount2,
    tootal
  };
};

再看第二个sandbox

相关问题