After hours of searchs and tests, I didn't succeed to find any solution to make flow understand defaultProps
See Try Flow example
Using this model is unconsitent:
type PropsType = {|
size?: 'small' | 'big',
|};
class Letter extends PureComponent<PropsType> {
static defaultProps = {
size: 'small',
}
}
Thanks
After Tomasz Mularczyk's suggestion, I created another Try Flow example with:
type PropsType = {|
size: 'small' | 'big',
|};
instead of
type PropsType = {|
size?: 'small' | 'big',
|};
But
defaultProp size
is not typed anymore:
static defaultProps = {
size: 'BAD DEFAULT PROPS', // no error (?!)
}
See Tomasz Mularczyk's answer
+
Don't wait flow error on your current component ( in my case), but where it will be imported and declared.(seems semi-static no?)
Because defaultProps on functions will eventually get deprecated.
If you want the default values to be applied for every method, such as the ones from the React life-cycle ( shouldComponentUpdate , etc.) then you must use the defaultProps instead. So, the previous code is actually a mistake that may lead to misunderstanding about the default value of name .
The defaultProps is a React component property that allows you to set default values for the props argument. If the prop property is passed, it will be changed. The defaultProps can be defined as a property on the component class itself, to set the default props for the class.
React. Node can be null, a boolean, a number, a string, a React element, or an array of any of those types recursively. If you need a return type for your component render() methods then you should use React.
Just remove ?
from size
type. Flow will automatically see that you created defaultProps
and will not require it.
From docs:
To type default props add a static defaultProps property to your class.
...
React also supports default props on stateless functional components. Similarly to class components, default props for stateless functional components will work without any extra type annotations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With