Skip to content

Commit ceb2c60

Browse files
author
Brian Vaughn
committed
Updated useReducer reducer example to show default case
1 parent 333656c commit ceb2c60

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

content/docs/hooks-reference.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ function reducer(state, action) {
190190
return {count: state.count + 1};
191191
case 'decrement':
192192
return {count: state.count - 1};
193+
default:
194+
// A reducer must always return a valid state.
195+
// Alternatively you can throw an error if an invalid action is dispatched.
196+
return state;
193197
}
194198
}
195199

@@ -223,6 +227,10 @@ function reducer(state, action) {
223227
return {count: state.count + 1};
224228
case 'decrement':
225229
return {count: state.count - 1};
230+
default:
231+
// A reducer must always return a valid state.
232+
// Alternatively you can throw an error if an invalid action is dispatched.
233+
return state;
226234
}
227235
}
228236

0 commit comments

Comments
 (0)