@@ -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
0 commit comments