@@ -52,6 +52,7 @@ This gives you the power to prioritize our work and support the project contribu
52
52
- [ Render Props] ( #render-props ) 🌟 __ NEW__
53
53
- [ Higher-Order Components] ( #higher-order-components ) 📝 __ UPDATED__
54
54
- [ Redux Connected Components] ( #redux-connected-components )
55
+ - [ Hooks] ( #hooks )
55
56
- [ Redux] ( #redux )
56
57
- [ Action Creators] ( #action-creators ) 📝 __ UPDATED__
57
58
- [ Reducers] ( #reducers ) 📝 __ UPDATED__
@@ -756,6 +757,66 @@ export default () => <SFCCounterConnectedExtended label={'SFCCounterConnectedExt
756
757
757
758
[⇧ back to top](#table-of-contents)
758
759
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
+
759
820
---
760
821
761
822
# Redux
0 commit comments