Skip to content

Commit 86902ac

Browse files
sosukesuzukipiotrwitek
authored andcommitted
Add Example for useReducer with types. (#118)
* Add example for useReducer * Update React * Fix text for useReducer document * Add "Hooks" to table content * Add the documentation for hooks to README * Decrale Action as typesage * Remvoe ^ prefix from package.json
1 parent 61127c1 commit 86902ac

File tree

6 files changed

+944
-875
lines changed

6 files changed

+944
-875
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ This gives you the power to prioritize our work and support the project contribu
5252
- [Render Props](#render-props) 🌟 __NEW__
5353
- [Higher-Order Components](#higher-order-components) 📝 __UPDATED__
5454
- [Redux Connected Components](#redux-connected-components)
55+
- [Hooks](#hooks)
5556
- [Redux](#redux)
5657
- [Action Creators](#action-creators) 📝 __UPDATED__
5758
- [Reducers](#reducers) 📝 __UPDATED__
@@ -756,6 +757,66 @@ export default () => <SFCCounterConnectedExtended label={'SFCCounterConnectedExt
756757
757758
[⇧ back to top](#table-of-contents)
758759
760+
## Hooks
761+
762+
> https://reactjs.org/docs/hooks-intro.html
763+
764+
#### - useReducer
765+
Hook for state management like Redux in a function component.
766+
767+
```tsx
768+
import * as React from 'react';
769+
770+
interface State {
771+
count: number;
772+
}
773+
774+
type Action =
775+
| { type: 'reset' }
776+
| { type: 'increment' }
777+
| { type: 'decrement' };
778+
779+
const initialState: State = {
780+
count: 0,
781+
};
782+
783+
function reducer(state: State, action: Action): State {
784+
switch (action.type) {
785+
case 'reset':
786+
return initialState;
787+
case 'increment':
788+
return { count: state.count + 1 };
789+
case 'decrement':
790+
return { count: state.count - 1 };
791+
default:
792+
return state;
793+
}
794+
}
795+
796+
interface CounterProps {
797+
initialCount: number;
798+
}
799+
800+
function Counter({ initialCount }: CounterProps) {
801+
const [state, dispatch] = React.useReducer<State, Action>(reducer, {
802+
count: initialCount,
803+
});
804+
return (
805+
<>
806+
Count: {state.count}
807+
<button onClick={() => dispatch({ type: 'reset' })}>Reset</button>
808+
<button onClick={() => dispatch({ type: 'increment' })}>+</button>
809+
<button onClick={() => dispatch({ type: 'decrement' })}>-</button>
810+
</>
811+
);
812+
}
813+
814+
export default Counter;
815+
816+
```
817+
818+
[⇧ back to top](#table-of-contents)
819+
759820
---
760821
761822
# Redux

docs/markdown/1_react.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,14 @@ const mapDispatchToProps = (dispatch: Dispatch<ActionType>) => ({
202202
::usage='../../playground/src/connected/sfc-counter-connected-extended.usage.tsx'::
203203
204204
[⇧ back to top](#table-of-contents)
205+
206+
## Hooks
207+
208+
> https://reactjs.org/docs/hooks-intro.html
209+
210+
#### - useReducer
211+
Hook for state management like Redux in a function component.
212+
213+
::example='../../playground/src/hooks/use-reducer.tsx'::
214+
215+
[⇧ back to top](#table-of-contents)

docs/markdown/_toc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [Render Props](#render-props) 🌟 __NEW__
99
- [Higher-Order Components](#higher-order-components) 📝 __UPDATED__
1010
- [Redux Connected Components](#redux-connected-components)
11+
- [Hooks](#hooks)
1112
- [Redux](#redux)
1213
- [Action Creators](#action-creators) 📝 __UPDATED__
1314
- [Reducers](#reducers) 📝 __UPDATED__

0 commit comments

Comments
 (0)