Skip to content

Add Example for useReducer with types. #118

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

Merged
merged 7 commits into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ This gives you the power to prioritize our work and support the project contribu
- [Render Props](#render-props) 🌟 __NEW__
- [Higher-Order Components](#higher-order-components) 📝 __UPDATED__
- [Redux Connected Components](#redux-connected-components)
- [Hooks](#hooks)
- [Redux](#redux)
- [Action Creators](#action-creators) 📝 __UPDATED__
- [Reducers](#reducers) 📝 __UPDATED__
Expand Down Expand Up @@ -755,6 +756,65 @@ export default () => <SFCCounterConnectedExtended label={'SFCCounterConnectedExt

[⇧ back to top](#table-of-contents)

## Hooks

> https://reactjs.org/docs/hooks-intro.html

#### - useReducer
Hook for state management like Redux in a function component.

```tsx
import * as React from 'react';

interface State {
count: number;
}

interface Action {
type: string;
}

const initialState: State = {
count: 0,
};

function reducer(state: State, action: Action): State {
switch (action.type) {
case 'reset':
return initialState;
case 'increment':
return { count: state.count + 1 };
case 'decrement':
return { count: state.count - 1 };
default:
return state;
}
}

interface CounterProps {
initialCount: number;
}

function Counter({ initialCount }: CounterProps) {
const [state, dispatch] = React.useReducer<State, Action>(reducer, {
count: initialCount,
});
return (
<>
Count: {state.count}
<button onClick={() => dispatch({ type: 'reset' })}>Reset</button>
<button onClick={() => dispatch({ type: 'increment' })}>+</button>
<button onClick={() => dispatch({ type: 'decrement' })}>-</button>
</>
);
}

export default Counter;

```

[⇧ back to top](#table-of-contents)

---

# Redux
Expand Down
11 changes: 11 additions & 0 deletions docs/markdown/1_react.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,14 @@ const mapDispatchToProps = (dispatch: Dispatch<ActionType>) => ({
::usage='../../playground/src/connected/sfc-counter-connected-extended.usage.tsx'::

[⇧ back to top](#table-of-contents)

## Hooks

> https://reactjs.org/docs/hooks-intro.html

#### - useReducer
Hook for state management like Redux in a function component.

::example='../../playground/src/hooks/use-reducer.tsx'::

[⇧ back to top](#table-of-contents)
1 change: 1 addition & 0 deletions docs/markdown/_toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [Render Props](#render-props) 🌟 __NEW__
- [Higher-Order Components](#higher-order-components) 📝 __UPDATED__
- [Redux Connected Components](#redux-connected-components)
- [Hooks](#hooks)
- [Redux](#redux)
- [Action Creators](#action-creators) 📝 __UPDATED__
- [Reducers](#reducers) 📝 __UPDATED__
Expand Down
Loading