-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix: @connect decorator #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
obsofficer-ctrl
wants to merge
10
commits into
piotrwitek:master
Choose a base branch
from
obsofficer-ctrl:fix/task-311618049
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8b48b2e
Add playground\src\connected\connected-counter.tsx
obsofficer-ctrl fa87d22
Add playground\src\connected\connected-counter-with-decorator.tsx
obsofficer-ctrl f3c7017
Add playground\src\features\counters\index.ts
obsofficer-ctrl ee6535d
Update playground\src\connected\connected-counter-with-decorator.tsx
obsofficer-ctrl fbfc3b6
Update playground\src\connected\connected-counter-with-decorator.tsx
obsofficer-ctrl 268213a
Add playground\tsconfig.json
obsofficer-ctrl ebc7315
Update playground\src\connected\connected-counter-with-decorator.tsx
obsofficer-ctrl dc2edcb
Update playground\src\connected\connected-counter-with-decorator.tsx
obsofficer-ctrl 48d552d
Update playground\src\connected\connected-counter-with-decorator.tsx
obsofficer-ctrl 578f47f
Update playground\src\connected\connected-counter-with-decorator.tsx
obsofficer-ctrl 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
66 changes: 66 additions & 0 deletions
66
playground\src\connected\connected-counter-with-decorator.tsx
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,66 @@ | ||
| // NOTE: Requires `"experimentalDecorators": true` in tsconfig.json | ||
| import React, { Component } from 'react'; | ||
| import { connect } from 'react-redux'; | ||
| import { bindActionCreators, Dispatch } from 'redux'; | ||
| import { RootState } from '../store/types'; | ||
| import { countersActions } from '../features/counters'; | ||
|
|
||
| // Props passed by the parent component | ||
| interface OwnProps { | ||
| label: string; | ||
| } | ||
|
|
||
| // Props mapped from Redux state via mapStateToProps | ||
| interface StateProps { | ||
| count: number; | ||
| } | ||
|
|
||
| // Props mapped from Redux dispatch via mapDispatchToProps | ||
| interface DispatchProps { | ||
| onIncrement: () => void; | ||
| onDecrement: () => void; | ||
| } | ||
|
|
||
| // All props combined — used as the component Props type | ||
| type Props = OwnProps & StateProps & DispatchProps; | ||
|
|
||
| const mapStateToProps = ( | ||
| state: RootState, | ||
| _ownProps: OwnProps | ||
| ): StateProps => ({ | ||
| count: state.counters.reduxCounter, | ||
| }); | ||
|
|
||
| const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => | ||
| bindActionCreators( | ||
| { | ||
| onIncrement: countersActions.increment, | ||
| onDecrement: countersActions.decrement, | ||
| }, | ||
| dispatch | ||
| ); | ||
|
|
||
| // Using @connect decorator | ||
| // Equivalent to: | ||
| // export default connect(mapStateToProps, mapDispatchToProps)(ConnectedCounterWithDecorator) | ||
| @connect<StateProps, DispatchProps, OwnProps, RootState>( | ||
| mapStateToProps, | ||
| mapDispatchToProps | ||
| ) | ||
| class ConnectedCounterWithDecorator extends Component<Props> { | ||
| render() { | ||
| const { label, count, onIncrement, onDecrement } = this.props; | ||
|
|
||
| return ( | ||
| <div> | ||
| <span> | ||
| {label}: {count} | ||
| </span> | ||
| <button onClick={onIncrement}>+</button> | ||
| <button onClick={onDecrement}>-</button> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default ConnectedCounterWithDecorator; |
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,59 @@ | ||||||
| import React, { Component } from 'react'; | ||||||
| import { connect } from 'react-redux'; | ||||||
| import { bindActionCreators, Dispatch } from 'redux'; | ||||||
| import { RootState } from '../store/types'; | ||||||
| import { countersActions } from '../features/counters'; | ||||||
| import { hotelsActions } from '../features/hotels'; | ||||||
|
|
||||||
| // Props that come from the parent component | ||||||
| interface OwnProps { | ||||||
| label: string; | ||||||
| } | ||||||
|
|
||||||
| // Props from Redux state (mapStateToProps) | ||||||
| interface StateProps { | ||||||
| count: number; | ||||||
| } | ||||||
|
|
||||||
| // Props from Redux dispatch (mapDispatchToProps) | ||||||
| interface DispatchProps { | ||||||
| onIncrement: () => void; | ||||||
| onDecrement: () => void; | ||||||
| } | ||||||
|
|
||||||
| // All component props combined | ||||||
| type Props = OwnProps & StateProps & DispatchProps; | ||||||
|
|
||||||
| // Map Redux state to component props | ||||||
| const mapStateToProps = (state: RootState, ownProps: OwnProps): StateProps => ({ | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
| count: state.counters.reduxCounter, | ||||||
| }); | ||||||
|
|
||||||
| // Map Redux dispatch to component props | ||||||
| const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => | ||||||
| bindActionCreators( | ||||||
| { | ||||||
| onIncrement: countersActions.increment, | ||||||
| onDecrement: countersActions.decrement, | ||||||
| }, | ||||||
| dispatch | ||||||
| ); | ||||||
|
|
||||||
| // Using the @connect decorator syntax | ||||||
| @connect(mapStateToProps, mapDispatchToProps) | ||||||
| class ConnectedCounter extends Component<Props> { | ||||||
| render() { | ||||||
| const { label, count, onIncrement, onDecrement } = this.props; | ||||||
| return ( | ||||||
| <div> | ||||||
| <span> | ||||||
| {label}: {count} | ||||||
| </span> | ||||||
| <button onClick={onIncrement}>+</button> | ||||||
| <button onClick={onDecrement}>-</button> | ||||||
| </div> | ||||||
| ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export default ConnectedCounter; | ||||||
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,3 @@ | ||
| export { countersReducer } from './reducer'; | ||
| export { countersActions } from './actions'; | ||
| export { CounterState } from './types'; |
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,27 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "es6", | ||
| "lib": [ | ||
| "dom", | ||
| "dom.iterable", | ||
| "esnext" | ||
| ], | ||
| "allowJs": true, | ||
| "skipLibCheck": true, | ||
| "esModuleInterop": true, | ||
| "allowSyntheticDefaultImports": true, | ||
| "strict": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "module": "esnext", | ||
| "moduleResolution": "node", | ||
| "resolveJsonModule": true, | ||
| "isolatedModules": true, | ||
| "noEmit": true, | ||
| "jsx": "react", | ||
| "experimentalDecorators": true, | ||
| "baseUrl": "." | ||
| }, | ||
| "include": [ | ||
| "src" | ||
| ] | ||
| } |
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.
The
hotelsActionsimport is not used in this file and should be removed to maintain code cleanliness.