CalebMorris / react-moment-proptypes

React proptype validator for moment.js
49 stars 17 forks source link

Invalid prop of type `Object`, expected instance of `bound propValidator` #34

Closed mayacode closed 6 years ago

mayacode commented 6 years ago

Hi,

I just added moment and moment props to my project and am facing an error: Failed prop type: Invalid prop 'prop1' of type 'Object' supplied to 'Component1', expected instance of 'bound propValidator'.

dependencies:

"react": "^15.6",
"moment": "^2.22.1",
"moment-timezone": "^0.5.15",
"react-moment-proptypes": "^1.5.0",

code:

import moment from 'moment';
import React, {Component} from 'react';
import momentProps from 'react-moment-proptypes';

export default class Component1 extends Component {
  static propTypes = {
      accessPeriodFrom: instanceOf(momentProps.momentObj),
      accessPeriodTo: instanceOf(momentProps.momentObj),
  };

  static defaultProps = {
    accessPeriodFrom: null,
    accessPeriodTo: null,
  }

  render() {
    // some code
  }
}

As long as dates come as null or undefined everything is OK, in moment, when fetching data gives result and to component come moment objects (I check them with moment().isMoment() in container, from which they are passed to dumb Component1) I see the error.

CalebMorris commented 6 years ago

In your static propTypes block you have accessPeriodFrom: instanceOf(momentProps.momentObj). I'm not sure what the purpose of having instanceOf is supposed to do here. It looks like it would prevent passing the actual propType.
Can you change to:

static propTypes = {
      accessPeriodFrom: momentProps.momentObj,
      accessPeriodTo: momentProps.momentObj,
  };

and see if it fixes your problem?

CalebMorris commented 6 years ago

Haven't heard back since April and I'm unable to reproduce or reject my hypothesis without more info.
Closing for now. If my suggestion didn't fix it or you're able to give more details feel free to clone or re-open?