Browse Source
Merge pull request #1386 from reactjs/useReducer-example
Updated useReducer reducer example to show default case
main
Brian Vaughn
6 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with
8 additions and
0 deletions
-
content/docs/hooks-reference.md
|
@ -190,6 +190,10 @@ function reducer(state, action) { |
|
|
return {count: state.count + 1}; |
|
|
return {count: state.count + 1}; |
|
|
case 'decrement': |
|
|
case 'decrement': |
|
|
return {count: state.count - 1}; |
|
|
return {count: state.count - 1}; |
|
|
|
|
|
default: |
|
|
|
|
|
// A reducer must always return a valid state. |
|
|
|
|
|
// Alternatively you can throw an error if an invalid action is dispatched. |
|
|
|
|
|
return state; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -223,6 +227,10 @@ function reducer(state, action) { |
|
|
return {count: state.count + 1}; |
|
|
return {count: state.count + 1}; |
|
|
case 'decrement': |
|
|
case 'decrement': |
|
|
return {count: state.count - 1}; |
|
|
return {count: state.count - 1}; |
|
|
|
|
|
default: |
|
|
|
|
|
// A reducer must always return a valid state. |
|
|
|
|
|
// Alternatively you can throw an error if an invalid action is dispatched. |
|
|
|
|
|
return state; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|