Skip to content

Commit 65fbf77

Browse files
authored
Add useSelector and useDispatch example (#207)
1 parent e0532ae commit 65fbf77

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ I highly recommend to add a bounty to the issue that you're waiting for to incre
106106
- [Redux Connected Components](#redux-connected-components)
107107
- [- Redux connected counter](#--redux-connected-counter)
108108
- [- Redux connected counter with own props](#--redux-connected-counter-with-own-props)
109+
- [- Redux connected counter via hooks](#--redux-connected-counter-via-hooks)
109110
- [- Redux connected counter with `redux-thunk` integration](#--redux-connected-counter-with-redux-thunk-integration)
110111
- [Context](#context)
111112
- [ThemeContext](#themecontext)
@@ -132,6 +133,7 @@ I highly recommend to add a bounty to the issue that you're waiting for to incre
132133
- [Selectors with `reselect`](#selectors-with-reselect)
133134
- [Connect with `react-redux`](#connect-with-react-redux)
134135
- [Typing connected component](#typing-connected-component)
136+
- [Typing `useSelector` and `useDispatch`](#typing-useselector-and-usedispatch)
135137
- [Typing connected component with `redux-thunk` integration](#typing-connected-component-with-redux-thunk-integration)
136138
- [Configuration & Dev Tools](#configuration--dev-tools)
137139
- [Common Npm Scripts](#common-npm-scripts)
@@ -874,6 +876,26 @@ export default () => (
874876
875877
[⇧ back to top](#table-of-contents)
876878
879+
### - Redux connected counter via hooks
880+
881+
```tsx
882+
import * as React from 'react';
883+
import { FCCounter } from '../components';
884+
import { increment } from '../features/counters/actions';
885+
import { useSelector, useDispatch } from '../store/hooks';
886+
887+
const FCCounterConnectedHooksUsage: React.FC = () => {
888+
const counter = useSelector(state => state.counters.reduxCounter);
889+
const dispatch = useDispatch();
890+
return <FCCounter label="Use selector" count={counter} onIncrement={() => dispatch(increment())}/>;
891+
};
892+
893+
export default FCCounterConnectedHooksUsage;
894+
895+
```
896+
897+
[⇧ back to top](#table-of-contents)
898+
877899
### - Redux connected counter with `redux-thunk` integration
878900
879901
```tsx
@@ -1703,6 +1725,27 @@ const mapDispatchToProps = (dispatch: Dispatch<MyTypes.RootAction>) =>
17031725

17041726
```
17051727
1728+
[⇧ back to top](#table-of-contents)
1729+
1730+
### Typing `useSelector` and `useDispatch`
1731+
1732+
```tsx
1733+
import { Dispatch } from 'redux';
1734+
import {
1735+
TypedUseSelectorHook,
1736+
useSelector as useGenericSelector,
1737+
useDispatch as useGenericDispatch
1738+
} from 'react-redux';
1739+
import { RootState, RootAction } from 'MyTypes';
1740+
1741+
export const useSelector: TypedUseSelectorHook<RootState> = useGenericSelector;
1742+
1743+
export const useDispatch: () => Dispatch<RootAction> = useGenericDispatch;
1744+
1745+
```
1746+
1747+
[⇧ back to top](#table-of-contents)
1748+
17061749
### Typing connected component with `redux-thunk` integration
17071750
17081751
*__NOTE__: When using thunk action creators you need to use `bindActionCreators`. Only this way you can get corrected dispatch props type signature like below.*

README_SOURCE.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ I highly recommend to add a bounty to the issue that you're waiting for to incre
106106
- [Redux Connected Components](#redux-connected-components)
107107
- [- Redux connected counter](#--redux-connected-counter)
108108
- [- Redux connected counter with own props](#--redux-connected-counter-with-own-props)
109+
- [- Redux connected counter via hooks](#--redux-connected-counter-via-hooks)
109110
- [- Redux connected counter with `redux-thunk` integration](#--redux-connected-counter-with-redux-thunk-integration)
110111
- [Context](#context)
111112
- [ThemeContext](#themecontext)
@@ -132,6 +133,7 @@ I highly recommend to add a bounty to the issue that you're waiting for to incre
132133
- [Selectors with `reselect`](#selectors-with-reselect)
133134
- [Connect with `react-redux`](#connect-with-react-redux)
134135
- [Typing connected component](#typing-connected-component)
136+
- [Typing `useSelector` and `useDispatch`](#typing-useselector-and-usedispatch)
135137
- [Typing connected component with `redux-thunk` integration](#typing-connected-component-with-redux-thunk-integration)
136138
- [Configuration & Dev Tools](#configuration--dev-tools)
137139
- [Common Npm Scripts](#common-npm-scripts)
@@ -374,6 +376,12 @@ Adds error handling using componentDidCatch to any component
374376
375377
[⇧ back to top](#table-of-contents)
376378
379+
### - Redux connected counter via hooks
380+
381+
::codeblock='playground/src/hooks/react-redux-hooks.tsx'::
382+
383+
[⇧ back to top](#table-of-contents)
384+
377385
### - Redux connected counter with `redux-thunk` integration
378386
379387
::codeblock='playground/src/connected/fc-counter-connected-bind-action-creators.tsx'::
@@ -641,6 +649,14 @@ const mapDispatchToProps = (dispatch: Dispatch<MyTypes.RootAction>) =>
641649

642650
```
643651
652+
[⇧ back to top](#table-of-contents)
653+
654+
### Typing `useSelector` and `useDispatch`
655+
656+
::codeblock='playground/src/store/hooks.ts'::
657+
658+
[⇧ back to top](#table-of-contents)
659+
644660
### Typing connected component with `redux-thunk` integration
645661
646662
*__NOTE__: When using thunk action creators you need to use `bindActionCreators`. Only this way you can get corrected dispatch props type signature like below.*
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as React from 'react';
2+
import { FCCounter } from '../components';
3+
import { increment } from '../features/counters/actions';
4+
import { useSelector, useDispatch } from '../store/hooks';
5+
6+
const FCCounterConnectedHooksUsage: React.FC = () => {
7+
const counter = useSelector(state => state.counters.reduxCounter);
8+
const dispatch = useDispatch();
9+
return <FCCounter label="Use selector" count={counter} onIncrement={() => dispatch(increment())}/>;
10+
};
11+
12+
export default FCCounterConnectedHooksUsage;

playground/src/store/hooks.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Dispatch } from 'redux';
2+
import {
3+
TypedUseSelectorHook,
4+
useSelector as useGenericSelector,
5+
useDispatch as useGenericDispatch
6+
} from 'react-redux';
7+
import { RootState, RootAction } from 'MyTypes';
8+
9+
export const useSelector: TypedUseSelectorHook<RootState> = useGenericSelector;
10+
11+
export const useDispatch: () => Dispatch<RootAction> = useGenericDispatch;

0 commit comments

Comments
 (0)