Flow: Flow type does not accept the defaultProps use react lazy

Created on 21 Jun 2019  路  1Comment  路  Source: facebook/flow

I haven't found the answer for a long time and I didn't get a response on stackoverflow so I had to go here

Can u help me

https://stackoverflow.com/questions/56073501/flow-type-does-not-accept-the-defaultprops-use-react-lazy

Flow type does not accept the defaultProps use react lazy.

This is my code:

1. defaultProps in Post Component:

static defaultProps = {
  featureImage: "",
  featureRatio: "16x9"
}

2. use component with React lazy

const PostLazy = lazy<PostProps>(
  (): any => import("../components/Post")
);

<PostLazy
  featureImage={item.featureImage}
/>

3. and I received the Flow error

Cannot create `PostLazy` element because property `featureRatio` is missing
in  props [1] but exists in  `PostProps` [2].

It works well when I use it in the usual way

import Post from "../components/Post";
question

Most helpful comment

You should use lazy<React.Config<Props, DefaultProps>> to keep default props optional. You'll also need to make a DefaultProps type alias for the class.

>All comments

You should use lazy<React.Config<Props, DefaultProps>> to keep default props optional. You'll also need to make a DefaultProps type alias for the class.

Was this page helpful?
0 / 5 - 0 ratings