Search code examples
infinite-loopredux-form

redux-form infinite loop when setting a date in initialValues


I get an infinite loop of @@redux-form/INITIALIZE messages when I try to initialise the value of a fabric ui datepicker field

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

If I replace new Date() with "" then no loop - but then no initialisation. React Newb

Update. Date() generates a different value each time it is called. Is that upsetting redux-form in some way? I have worked around he problem by setting the default directly in the fabric ui component for the time being


Solution

  • mapStateToProps is called every time when it's are updated, so if you pass new Date() as params it is predictably that your connected component will rerender each millisecond.

    Move new Date() to variable and than pass it to mapStateToProps.

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