JavaScript Optional function prop in React component Flow type check fail react optional function prop,typescript optional function prop,javascript function

I am using React's PropTypes and Flow type checker but I am having trouble getting an optional function prop to pass the type check. Here is an example:

var Example = React.createClass({
  propTypes: {
    exampleFn: React.PropTypes.func
  },

  handleClick: function() {
    if (this.props.exampleFn) {
      this.props.exampleFn();
    }
  },

  render: function() {
    return <a onClick={this.handleClick}>Click here</a>;
  }
});

Although I'm checking this.props.exampleFn is not null, running Flow's type checker against this code gives me the error

call of method exampleFn
Function cannot be called on possibly null or undefined value

I've tried different variations such as
if (this.props.exampleFn !== null && this.props.exampleFn !== undefined) {...}
or
this.props.exampleFn && this.props.exampleFn()
etc. to know that we're guarding against a possibly null/undefined value but I can't find anything that works. Of course, changing the prop type to React.PropTypes.func.isRequired results in no errors but I'd like to keep this prop optional.

How can I get an optional function prop to pass the type check?

Answer:1

I have a select drop down filled with options and then I have two radios like so... <form> <select id="drop_down"> <option value="1">One</option> <option value="2"...

I have a select drop down filled with options and then I have two radios like so... <form> <select id="drop_down"> <option value="1">One</option> <option value="2"...

I have an object similar to this: $scope.division = [ { name: "Tom", number: 1 }, { name: "Greg", number: 2 } ]; Now in my HTML I'm using ngOption to display a drop down ...

I have an object similar to this: $scope.division = [ { name: "Tom", number: 1 }, { name: "Greg", number: 2 } ]; Now in my HTML I'm using ngOption to display a drop down ...

I wish to have a row of 3 images which will contain around 10 images per row. The image dimensions are 270x270px and should animate like you'd imagine clouds floating from right to left on the screen....

I wish to have a row of 3 images which will contain around 10 images per row. The image dimensions are 270x270px and should animate like you'd imagine clouds floating from right to left on the screen....

We are trying to test our controller with Karma, but Karma keeps complaining about an unknown provider: Error: [$injector:unpr] Unknown provider: userProvider <- user Are we injecting the user ...

We are trying to test our controller with Karma, but Karma keeps complaining about an unknown provider: Error: [$injector:unpr] Unknown provider: userProvider <- user Are we injecting the user ...

  1. karma error spawn unknown
  2. karma-server error spawn unknown