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

redux-form infinite loop when setting a date in initialValues

当我尝试初始化 fabric ui 日期选择器字段

的值时,出现无限循环的@@redux-form/INITIALIZE 消息
function mapStateToProps(state) {
  const { bookingform } = state;
  return {
    initialValues: { date: new Date()},
    bookingform
  };
}

如果我将 new Date() 替换为“”,则没有循环 - 但没有初始化。 React Newb

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

mapStateToProps 每次更新时都会被调用,因此如果您将 new Date() 作为参数传递,可以预见您的连接组件将每毫秒重新呈现一次。

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

const now = new Date();

function mapStateToProps(state) {
  const { bookingform } = state;
  return {
    initialValues: { date: now },
    bookingform
  };
}
export default Example = reduxForm({
  form: 'example',
  enableReinitialize: false  // set it to false
})(Example);