在initialValues中设置日期时,redux-form无限循环

问题描述 投票:1回答:2

当我尝试初始化fabric ui datepicker字段的值时,我得到一个无限循环的@@ redux-form / INITIALIZE消息

function mapStateToProps(state) {
  const { bookingform } = state;
  return {
    initialValues: { date: new Date()},
    bookingform
  };
}

如果我用“”替换新的Date()然后没有循环 - 但是没有初始化。 反应Newb

更新。 Date()每次调用时都会生成不同的值。 是否以某种方式扰乱了还原形式? 我通过直接在fabric ui组件中设置默认值来解决他的问题

infinite-loop redux-form
2个回答
2
投票

每次更新mapStateToProps都会调用它,因此如果将new Date()作为params传递,则可以预测连接的组件将每毫秒重新渲染一次。

new Date()移动到变量,然后将其传递给mapStateToProps

const now = new Date();

function mapStateToProps(state) {
  const { bookingform } = state;
  return {
    initialValues: { date: now },
    bookingform
  };
}

1
投票
export default Example = reduxForm({
  form: 'example',
  enableReinitialize: false  // set it to false
})(Example);
© www.soinside.com 2019 - 2024. All rights reserved.