You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -18,28 +18,104 @@ In general, to make Flow happy with connect after 0.85 is a two-phase fix. First
18
18
19
19
## Fixing the “implicitly instantiated” errors at calls of `connect`
20
20
21
-
To begin, you need:
21
+
> **Note:** We need`React.AbstractComponent` from Flow v0.89+
22
22
23
-
- Flow upgraded to v0.89+
24
-
-[Newest Flow Typed library definition for React Redux](https://github.com/flow-typed/flow-typed/blob/master/definitions/npm/react-redux_v5.x.x/flow_v0.89.x-/react-redux_v5.x.x.js)
23
+
### Annotating at function return
25
24
26
-
We include three major use cases for annotating `connect` with Flow:
25
+
The easiest way to annotate connected components is to annotate at function call return. To do this, we need to know to types of props in our components:
26
+
27
+
-`OwnProps`: likely contain or equal to what you need as the second parameter to `mapStateToProps`. If there are props that are not used by `mapStateToProps`, i.e., the props that "pass through", include them here in `OwnProps` as well
28
+
-`Props`: `OwnProps` plus the props passed in by `mapStateToProps` and `mapDispatchToProps`
29
+
30
+
> **Note:** Inexact objects don't spread nor `$Diff` very well. It is strongly recommended that you use exact objects for connected components all the time.
31
+
32
+
```js
33
+
type OwnProps = {|
34
+
passthrough: string,
35
+
forMapStateToProps: string
36
+
|}
37
+
38
+
type Props = {|
39
+
...OwnProps,
40
+
fromMapStateToProps: string,
41
+
dispatch1: () =>void
42
+
|}
43
+
```
44
+
45
+
With `OwnProps` and `Props` in figured out, we are now ready to annotate the connected components.
46
+
47
+
In _component definition_, annotate the props with `Props`. The component will have access to all the injected props from `connect`:
48
+
49
+
```jsx
50
+
import*asReactfrom'react'
51
+
52
+
constMyComponent= (props:Props) => (
53
+
<div onClick={props.dispatch1}>
54
+
{props.passthrough}
55
+
{props.fromMapStateToProps}
56
+
</div>
57
+
)
58
+
```
59
+
60
+
When we export, this is also when we normally call `connect`, annotate the exported component with _just_`OwnProps`:
61
+
62
+
```jsx
63
+
import*asReactfrom'react'
64
+
65
+
// const MyComponent = ...
66
+
67
+
exportdefault (connect(
68
+
mapStateToProps,
69
+
mapDispatchToProps
70
+
)(MyComponent):React.AbstractComponent<OwnProps>)
71
+
```
72
+
73
+
### Annotating by providing explicit type parameters
74
+
75
+
We may also annotate connected components by providing explicit type parameters at call of `connect` with the help of the [newest Flow Typed library definition for React Redux](https://github.com/flow-typed/flow-typed/blob/master/definitions/npm/react-redux_v5.x.x/flow_v0.89.x-/react-redux_v5.x.x.js). Note that this will also require Flow v0.89+.
76
+
77
+
The Flow Typed library definition declares `connect` as follows:
The libdef also contains a [glossary of the abbreviations](https://github.com/flow-typed/flow-typed/blob/master/definitions/npm/react-redux_v5.x.x/flow_v0.89.x-/react-redux_v5.x.x.js#L14) which decrypts the signature to:
For the most common ways to connect components, we won't need all of the parameters. Normally, we need only `OwnProps` and `Props` at the call of `connect`, and `State` at the definition of `mapStateToProps`.
95
+
96
+
We may use `_` ([what's this?](https://github.com/facebook/flow/commit/ec70da4510d3a092fa933081c083bd0e513d0518)) as placeholder at unused type parameter positions. A common `connect` call may look like this:
97
+
98
+
```jsx
99
+
connect<Props, OwnProps, _, _, _, _>(…)
100
+
```
101
+
102
+
We include examples for three major use cases of annotating `connect` with Flow:
27
103
28
104
- Connecting stateless component with `mapStateToProps`
29
105
- Connecting components with `mapDispatchToProps` of action creators
30
106
- Connecting components with `mapStateToProps` and `mapDispatchToProps` of action creators
31
107
32
-
### Connecting stateless component with `mapStateToProps`
108
+
#### Connecting stateless component with `mapStateToProps`
## Annotating nested higher order components with connect
192
+
### Annotating nested higher order components with connect
117
193
118
-
If you are at the unfortunate position where your component is wrapped with nested higher order component, it is probably more difficult to annotate by providing explicit type parameters, as doing so will probably require tediously deducing prop types at each layer:
It is probably easier if you annotate once at file export:
194
+
If you are at the unfortunate position where your component is wrapped with nested higher order component, it is probably more difficult to annotate by providing explicit type parameters, as doing so will probably require that you tediously take away props at each layer. It is agian easier to annotate at function return:
135
195
136
196
```jsx
197
+
typeOwnProps= {|
198
+
passthrough: number,
199
+
forMapStateToProps: string,
200
+
|}
137
201
typeProps= {|
138
-
passthrough: number
202
+
...OwnProps,
203
+
injectedA: string,
204
+
injectedB: string,
205
+
fromMapStateToProps: string,
206
+
dispatch1: (number) =>void,
207
+
dispatch2: () =>void,
139
208
|}
140
-
type AllProps = {
141
-
// spreaded props should be exact
142
-
...Props,
143
-
injectedPropA: string,
144
-
injectedPropB: boolean,
145
-
injectedPropC: number,
146
-
injectedPropD: any
147
-
}
148
209
149
-
constComponent= (props:AllProps) => {
210
+
constComponent= (props:Props) => {// annotate the component with all props including injected props
)(Component):React.AbstractComponent<OwnProps>) // export the connected component without injected props
159
227
```
160
228
161
229
## Benefits of this version
162
230
163
-
After fixing the implicit instantiation errors, Flow now is able to report errors on type mismatches cross connected components and provide accurate error messages:
231
+
After fixing the implicit instantiation errors, if your code contains mismatched types between connected components, the total number of errors may go _up_. This is the result of Flow's improved coverage. If you are using console output for the Flow errors, you may not be able to see those errors until you clear other errors. These additional errors are grouped together, all tied back to React Redux's library definition, and have friendly error messages that will pin point you to the lines of code to the errors.
164
232
165
233

166
234
167
235
## References
168
236
237
+
**Articles**
238
+
169
239
- [Asking for Required Annotations](https://medium.com/flow-type/asking-for-required-annotations-64d4f9c1edf8)
170
-
-[Flow Typed tests for React Redux `connect`](https://github.com/flow-typed/flow-typed/blob/master/definitions/npm/react-redux_v5.x.x/flow_v0.89.x-/test_connect.js#L449)
171
-
-[Quick Note Fixing `connect` FlowType Annotation after 0.89](https://dev.to/wgao19/quick-note-fixing-connect-flowtype-annotation-after-089-joi)
172
-
-[Connect Examples from Flow Typed tests](https://github.com/flow-typed/flow-typed/blob/master/definitions/npm/react-redux_v5.x.x/flow_v0.89.x-/test_connect.js#L156)
240
+
241
+
**Upgrading guides**
242
+
173
243
- [Ville's and Jordan Brown's guide: _Adding Type Parameters to Connect_](https://gist.github.com/jbrown215/f425203ef30fdc8a28c213b90ba7a794)
244
+
- [Quick Note Fixing `connect` FlowType Annotation after 0.89](https://dev.to/wgao19/quick-note-fixing-connect-flowtype-annotation-after-089-joi)
245
+
246
+
**Talks**
247
+
248
+
- [Flow Be Happy](https://engineers.sg/video/flow-be-happy-reactjs-singapore--3419) A talk on upgrading Flow past 0.85, [slides](https://flow-be-happy.netlify.com/)
249
+
250
+
**Others**
251
+
252
+
- [Flow Typed tests for React Redux `connect`](https://github.com/flow-typed/flow-typed/blob/master/definitions/npm/react-redux_v5.x.x/flow_v0.89.x-/test_connect.js)
174
253
- [flow-typed/#2946: Discussion after 0.85](https://github.com/flow-typed/flow-typed/issues/2946)
175
254
- Add support for Flow 0.89+: [#3012](https://github.com/flow-typed/flow-typed/pull/3035), [#3035](https://github.com/flow-typed/flow-typed/pull/3035)
0 commit comments