Robert Balicki
Robert Balicki

Reputation: 1633

Flow doesn't complain with incompatible ReactClass usage and JSX

I believe the following should be caught by flow:

type MyProps = {
  foo: boolean,
};
const makeComponent = (C: ReactClass<MyProps>) => <C />;

From reading the source, I believe I am understanding ReactClass correctly.

What gives? This seems to also work with React.createElement(C, {})

On the other hand, the following breaks:

import MyComponent from '...'; // this component has props MyProps
const makeComponent = () => <MyComponent />;
// and likewise with React.createElement

Upvotes: 0

Views: 85

Answers (1)

Ross Solomon
Ross Solomon

Reputation: 665

According to this comment, ReactClass is buggy at best and should not be used. You can use the following instead:

type MyProps = {
  foo: boolean,
};
const makeComponent = (C: Class<React.Component<void, MyProps, void>>) => <C />;

Note that the parameters for React.Component are defaultProps, Props, and State. In the above example it is assumed that the component does not have either defaultProps or State defined, hence the void values.

Upvotes: 2

Related Questions