Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Update dependencies and react types and simplified use reducer example
  • Loading branch information
piotrwitek committed Feb 12, 2019
commit 3245d2f60389e0fb87488bd7749066652c464baa
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -888,25 +888,18 @@ interface State {
count: number;
}

type Action =
| { type: 'reset' }
| { type: 'increment' }
| { type: 'decrement' };

const initialState: State = {
count: 0,
};
type Action = { type: 'reset' } | { type: 'increment' } | { type: 'decrement' };

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 };
case 'reset':
return { count: 0 };
default:
return state;
throw new Error();
}
}

Expand All @@ -915,9 +908,10 @@ interface CounterProps {
}

function Counter({ initialCount }: CounterProps) {
const [state, dispatch] = React.useReducer<State, Action>(reducer, {
const [state, dispatch] = React.useReducer(reducer, {
count: initialCount,
});

return (
<>
Count: {state.count}
Expand Down
Loading