[react-redux] DefaultProps support
See original GitHub issueI was trying to add connect
to a component that has defaultProps
and I had a hard time getting it right, even thought I could.
One thing I noticed while looking for ways to do it, is that there is an issue on the current tests: https://github.com/flow-typed/flow-typed/blob/master/definitions/npm/react-redux_v5.x.x/flow_v0.89.x-/test_connect.js#L10
That line should be passthroughWithDefaultProp: number,
(not optional) because that component has passthroughWithDefaultProp
as a defaultProp.
When trying to use passthroughWithDefaultProp
inside that component, even though that prop is always a number (guaranteed by defaultProp) Flow will complain that it can be a number or undefined, which is not ideal.
Also note that on this Flow documentation example foo
is declared as number
and not an options `number: https://flow.org/en/docs/react/hoc/#toc-exporting-wrapped-components
Here is how I made it work, but maybe there is a way to avoid having to use React.Config
ourselves?:
import * as React from 'react';
type OwnProps = {|
x: number
|};
type Props = {|
...OwnProps,
// no state props
|};
class Comp extends React.Component<Props> {
static defaultProps = { x: 3 };
...
}
type DefaultProps = {| x: number |};
type Config = React.Config<Props, DefaultProps>;
type OwnConfig = React.Config<OwnProps, DefaultProps>;
connect<Config, OwnConfig, _, _, _, _>()(Comp);
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (5 by maintainers)
Top GitHub Comments
Nice work @fabiomcosta, would you mind documenting this somewhere useful? Maybe in the react-redux libdef?
In the way Flow models react,
Props
should be the type ofthis.props
andConfig
is the type of the Props with all defaultProps marked optional. The way you handled it in your example at the bottom of this issue is exactly correct!