This repository was archived by the owner on Jan 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Add typing a container doc #143
Open
yleflour
wants to merge
1
commit into
master
Choose a base branch
from
doc/typing-a-container
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# [Standard] Flow Typing a container | ||
|
||
## Owner: [Yann Leflour](https://github.com/yleflour) | ||
|
||
## Checks | ||
|
||
- [ ] The component's ownprops have to be typed in `TOWnProps` | ||
- [ ] The containers exposes its resulting props through a `TContainerProps` type | ||
- [ ] Your component uses your `TContainerProps` in its `TProps` | ||
|
||
## How | ||
|
||
### 0. Add a `$Return` utils type (1 min) | ||
|
||
- In your flowtypes folder a _utils.js_ folder | ||
- Add the following type, it will help you extract the result of a function | ||
|
||
``` | ||
// flowtypes/utils.js | ||
// @flow | ||
|
||
type Return_<R, F: (...args: Array<any>) => R> = R; | ||
declare type $Return<T> = Return_<*, T>; | ||
|
||
``` | ||
|
||
### 1. Type your ownprops (2 min) | ||
|
||
- In your _mycomponent.container.js_ file, declare your ownprops type. Those are the props required by the mapStateToProps or mapDispatchToProps methods. | ||
|
||
``` | ||
// mycomponent.container.js | ||
// @flow | ||
|
||
type TOwnProps = { | ||
myItemId: string, | ||
} | ||
|
||
const mapStateToProps = (state: TRootState, ownProps: TOwnProps) => ({...}); | ||
const mapDispatchToProps = (dispatch: Dispatch, ownProps: TOwnProps) => ({...}); | ||
``` | ||
|
||
### 2. Type your _mapStateToProps_ (2 min) | ||
|
||
- In your _mycomponent.container.js_ once you've written the _mapStateToProps_ method, declare your _TStateProps_ | ||
|
||
``` | ||
// mycomponent.container.js | ||
// @flow | ||
|
||
const mapStateToProps = (state: TRootState, ownProps: TOwnProps) => ({...}); | ||
|
||
type TStateProps = $Return<typeof mapStateToProps>; | ||
``` | ||
|
||
### 3. Type your _mapDispatchToProps_ (2 min) | ||
|
||
- In your _mycomponent.container.js_ once you've written the _mapDispatchToProps_ method, declare your _TDispatchProps_ | ||
|
||
``` | ||
// mycomponent.container.js | ||
// @flow | ||
|
||
const mapDispatchToProps1 = (state: TRootState, ownProps: TOwnProps) => ({...}); | ||
|
||
type TDispatchProps1 = $Return<typeof mapStateToProps1>; | ||
|
||
// OR | ||
|
||
const mapDispatchToProps2 = {...}; | ||
|
||
type TDispatchProps2 = typeof mapStateToProps2; | ||
``` | ||
|
||
### 4. Type your _TContainerProps_ (2 min) | ||
|
||
- In your _mycomponent.container.js_ declare your _TContainerProps_ and export them | ||
|
||
``` | ||
// mycomponent.container.js | ||
// @flow | ||
|
||
export type TContainerProps = TStateProps & TDispatchProps; | ||
``` | ||
|
||
### 5. Type your TProps (1 min) | ||
|
||
- In your _mycomponent.component.js_ use your _TContainerProps_ | ||
|
||
``` | ||
// mycomponent.component.js | ||
// @flow | ||
|
||
import { type TContainerProps } from './mycomponent.container.js'; | ||
|
||
type TProps = TContainerProps & { | ||
// Add other component required props here | ||
} | ||
|
||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c'est pas plutôt
import type { TContainerProps } from './mycomponent.container.js';
la syntaxe ?