Skip to content

Commit c8cd152

Browse files
author
Manish Paudel
committed
rename action type from incremented_age to increment_age for clarity
1 parent c0c955e commit c8cd152

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/content/reference/react/useReducer.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ The `dispatch` function returned by `useReducer` lets you update the state to a
6565
const [state, dispatch] = useReducer(reducer, { age: 42 });
6666

6767
function handleClick() {
68-
dispatch({ type: 'incremented_age' });
68+
dispatch({ type: 'increment_age' });
6969
// ...
7070
```
7171
@@ -116,7 +116,7 @@ To update what's on the screen, call <CodeStep step={2}>`dispatch`</CodeStep> wi
116116
117117
```js [[2, 2, "dispatch"]]
118118
function handleClick() {
119-
dispatch({ type: 'incremented_age' });
119+
dispatch({ type: 'increment_age' });
120120
}
121121
```
122122
@@ -128,7 +128,7 @@ React will pass the current state and the action to your <CodeStep step={4}>redu
128128
import { useReducer } from 'react';
129129

130130
function reducer(state, action) {
131-
if (action.type === 'incremented_age') {
131+
if (action.type === 'increment_age') {
132132
return {
133133
age: state.age + 1
134134
};
@@ -142,7 +142,7 @@ export default function Counter() {
142142
return (
143143
<>
144144
<button onClick={() => {
145-
dispatch({ type: 'incremented_age' })
145+
dispatch({ type: 'increment_age' })
146146
}}>
147147
Increment age
148148
</button>
@@ -177,7 +177,7 @@ Then you need to fill in the code that will calculate and return the next state.
177177
```js {4-7,10-13}
178178
function reducer(state, action) {
179179
switch (action.type) {
180-
case 'incremented_age': {
180+
case 'increment_age': {
181181
return {
182182
name: state.name,
183183
age: state.age + 1
@@ -201,7 +201,7 @@ function Form() {
201201
const [state, dispatch] = useReducer(reducer, { name: 'Taylor', age: 42 });
202202
203203
function handleButtonClick() {
204-
dispatch({ type: 'incremented_age' });
204+
dispatch({ type: 'increment_age' });
205205
}
206206
207207
function handleInputChange(e) {
@@ -224,7 +224,7 @@ State is read-only. Don't modify any objects or arrays in state:
224224
```js {4,5}
225225
function reducer(state, action) {
226226
switch (action.type) {
227-
case 'incremented_age': {
227+
case 'increment_age': {
228228
// 🚩 Don't mutate an object in state like this:
229229
state.age = state.age + 1;
230230
return state;
@@ -236,7 +236,7 @@ Instead, always return new objects from your reducer:
236236
```js {4-8}
237237
function reducer(state, action) {
238238
switch (action.type) {
239-
case 'incremented_age': {
239+
case 'increment_age': {
240240
// ✅ Instead, return a new object
241241
return {
242242
...state,
@@ -262,7 +262,7 @@ import { useReducer } from 'react';
262262

263263
function reducer(state, action) {
264264
switch (action.type) {
265-
case 'incremented_age': {
265+
case 'increment_age': {
266266
return {
267267
name: state.name,
268268
age: state.age + 1
@@ -284,7 +284,7 @@ export default function Form() {
284284
const [state, dispatch] = useReducer(reducer, initialState);
285285

286286
function handleButtonClick() {
287-
dispatch({ type: 'incremented_age' });
287+
dispatch({ type: 'increment_age' });
288288
}
289289

290290
function handleInputChange(e) {
@@ -947,7 +947,7 @@ Calling the `dispatch` function **does not change state in the running code**:
947947
function handleClick() {
948948
console.log(state.age); // 42
949949

950-
dispatch({ type: 'incremented_age' }); // Request a re-render with 43
950+
dispatch({ type: 'increment_age' }); // Request a re-render with 43
951951
console.log(state.age); // Still 42!
952952

953953
setTimeout(() => {
@@ -961,7 +961,7 @@ This is because [states behaves like a snapshot.](/learn/state-as-a-snapshot) Up
961961
If you need to guess the next state value, you can calculate it manually by calling the reducer yourself:
962962
963963
```js
964-
const action = { type: 'incremented_age' };
964+
const action = { type: 'increment_age' };
965965
dispatch(action);
966966

967967
const nextState = reducer(state, action);
@@ -978,7 +978,7 @@ React will **ignore your update if the next state is equal to the previous state
978978
```js {4-5,9-10}
979979
function reducer(state, action) {
980980
switch (action.type) {
981-
case 'incremented_age': {
981+
case 'increment_age': {
982982
// 🚩 Wrong: mutating existing object
983983
state.age++;
984984
return state;
@@ -998,7 +998,7 @@ You mutated an existing `state` object and returned it, so React ignored the upd
998998
```js {4-8,11-15}
999999
function reducer(state, action) {
10001000
switch (action.type) {
1001-
case 'incremented_age': {
1001+
case 'increment_age': {
10021002
// ✅ Correct: creating a new object
10031003
return {
10041004
...state,
@@ -1026,7 +1026,7 @@ Make sure that every `case` branch **copies all of the existing fields** when re
10261026
```js {5}
10271027
function reducer(state, action) {
10281028
switch (action.type) {
1029-
case 'incremented_age': {
1029+
case 'increment_age': {
10301030
return {
10311031
...state, // Don't forget this!
10321032
age: state.age + 1
@@ -1046,7 +1046,7 @@ If your state unexpectedly becomes `undefined`, you're likely forgetting to `ret
10461046
```js {10}
10471047
function reducer(state, action) {
10481048
switch (action.type) {
1049-
case 'incremented_age': {
1049+
case 'increment_age': {
10501050
// ...
10511051
}
10521052
case 'edited_name': {

0 commit comments

Comments
 (0)