Skip to content

Commit 813b549

Browse files
fix: global mutation avoided except for first example to make a point, addresses issue#7497
1 parent 55d1f8f commit 813b549

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/content/learn/updating-arrays-in-state.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,8 @@ Now it works correctly:
106106
```js
107107
import { useState } from 'react';
108108

109-
let nextId = 0;
110-
111109
export default function List() {
110+
const [nextId, setNextId] = useState(0);
112111
const [name, setName] = useState('');
113112
const [artists, setArtists] = useState([]);
114113

@@ -122,8 +121,9 @@ export default function List() {
122121
<button onClick={() => {
123122
setArtists([
124123
...artists,
125-
{ id: nextId++, name: name }
124+
{ id: nextId, name: name }
126125
]);
126+
setNextId(nextId + 1);
127127
}}>Add</button>
128128
<ul>
129129
{artists.map(artist => (
@@ -343,14 +343,14 @@ In this example, the Insert button always inserts at the index `1`:
343343
```js
344344
import { useState } from 'react';
345345

346-
let nextId = 3;
347346
const initialArtists = [
348347
{ id: 0, name: 'Marta Colvin Andrade' },
349348
{ id: 1, name: 'Lamidi Olonade Fakeye'},
350349
{ id: 2, name: 'Louise Nevelson'},
351350
];
352351

353352
export default function List() {
353+
const [nextId, setNextId] = useState(3);
354354
const [name, setName] = useState('');
355355
const [artists, setArtists] = useState(
356356
initialArtists
@@ -362,11 +362,12 @@ export default function List() {
362362
// Items before the insertion point:
363363
...artists.slice(0, insertAt),
364364
// New item:
365-
{ id: nextId++, name: name },
365+
{ id: nextId, name: name },
366366
// Items after the insertion point:
367367
...artists.slice(insertAt)
368368
];
369369
setArtists(nextArtists);
370+
setNextId(nextId + 1);
370371
setName('');
371372
}
372373

@@ -466,14 +467,14 @@ In this example, two separate artwork lists have the same initial state. They ar
466467
```js
467468
import { useState } from 'react';
468469

469-
let nextId = 3;
470470
const initialList = [
471471
{ id: 0, title: 'Big Bellies', seen: false },
472472
{ id: 1, title: 'Lunar Landscape', seen: false },
473473
{ id: 2, title: 'Terracotta Army', seen: true },
474474
];
475475

476476
export default function BucketList() {
477+
const [nextId, setNextId] = useState(3);
477478
const [myList, setMyList] = useState(initialList);
478479
const [yourList, setYourList] = useState(
479480
initialList
@@ -573,14 +574,14 @@ With this approach, none of the existing state items are being mutated, and the
573574
```js
574575
import { useState } from 'react';
575576

576-
let nextId = 3;
577577
const initialList = [
578578
{ id: 0, title: 'Big Bellies', seen: false },
579579
{ id: 1, title: 'Lunar Landscape', seen: false },
580580
{ id: 2, title: 'Terracotta Army', seen: true },
581581
];
582582

583583
export default function BucketList() {
584+
const [nextId, setNextId] = useState(3);
584585
const [myList, setMyList] = useState(initialList);
585586
const [yourList, setYourList] = useState(
586587
initialList
@@ -669,14 +670,14 @@ Here is the Art Bucket List example rewritten with Immer:
669670
import { useState } from 'react';
670671
import { useImmer } from 'use-immer';
671672

672-
let nextId = 3;
673673
const initialList = [
674674
{ id: 0, title: 'Big Bellies', seen: false },
675675
{ id: 1, title: 'Lunar Landscape', seen: false },
676676
{ id: 2, title: 'Terracotta Army', seen: true },
677677
];
678678

679679
export default function BucketList() {
680+
const [nextId, setNextId] = useState(3);
680681
const [myList, updateMyList] = useImmer(
681682
initialList
682683
);
@@ -1088,24 +1089,25 @@ import { useState } from 'react';
10881089
import AddTodo from './AddTodo.js';
10891090
import TaskList from './TaskList.js';
10901091

1091-
let nextId = 3;
10921092
const initialTodos = [
10931093
{ id: 0, title: 'Buy milk', done: true },
10941094
{ id: 1, title: 'Eat tacos', done: false },
10951095
{ id: 2, title: 'Brew tea', done: false },
10961096
];
10971097

10981098
export default function TaskApp() {
1099+
const [nextId, setNextId] = useState(3);
10991100
const [todos, setTodos] = useState(
11001101
initialTodos
11011102
);
11021103

11031104
function handleAddTodo(title) {
11041105
todos.push({
1105-
id: nextId++,
1106+
id: nextId,
11061107
title: title,
11071108
done: false
11081109
});
1110+
setNextId(nextId + 1);
11091111
}
11101112

11111113
function handleChangeTodo(nextTodo) {
@@ -1251,14 +1253,14 @@ import { useState } from 'react';
12511253
import AddTodo from './AddTodo.js';
12521254
import TaskList from './TaskList.js';
12531255

1254-
let nextId = 3;
12551256
const initialTodos = [
12561257
{ id: 0, title: 'Buy milk', done: true },
12571258
{ id: 1, title: 'Eat tacos', done: false },
12581259
{ id: 2, title: 'Brew tea', done: false },
12591260
];
12601261

12611262
export default function TaskApp() {
1263+
const [nextId, setNextId] = useState(3);
12621264
const [todos, setTodos] = useState(
12631265
initialTodos
12641266
);
@@ -1267,11 +1269,12 @@ export default function TaskApp() {
12671269
setTodos([
12681270
...todos,
12691271
{
1270-
id: nextId++,
1272+
id: nextId,
12711273
title: title,
12721274
done: false
12731275
}
12741276
]);
1277+
setNextId(nextId + 1);
12751278
}
12761279

12771280
function handleChangeTodo(nextTodo) {
@@ -1422,24 +1425,25 @@ import { useImmer } from 'use-immer';
14221425
import AddTodo from './AddTodo.js';
14231426
import TaskList from './TaskList.js';
14241427

1425-
let nextId = 3;
14261428
const initialTodos = [
14271429
{ id: 0, title: 'Buy milk', done: true },
14281430
{ id: 1, title: 'Eat tacos', done: false },
14291431
{ id: 2, title: 'Brew tea', done: false },
14301432
];
14311433

14321434
export default function TaskApp() {
1435+
const [nextId, setNextId] = useState(3);
14331436
const [todos, setTodos] = useState(
14341437
initialTodos
14351438
);
14361439

14371440
function handleAddTodo(title) {
14381441
todos.push({
1439-
id: nextId++,
1442+
id: nextId,
14401443
title: title,
14411444
done: false
14421445
});
1446+
setNextId(nextId + 1);
14431447
}
14441448

14451449
function handleChangeTodo(nextTodo) {
@@ -1604,26 +1608,27 @@ import { useImmer } from 'use-immer';
16041608
import AddTodo from './AddTodo.js';
16051609
import TaskList from './TaskList.js';
16061610

1607-
let nextId = 3;
16081611
const initialTodos = [
16091612
{ id: 0, title: 'Buy milk', done: true },
16101613
{ id: 1, title: 'Eat tacos', done: false },
16111614
{ id: 2, title: 'Brew tea', done: false },
16121615
];
16131616

16141617
export default function TaskApp() {
1618+
const [nextId, setNextId] = useState(3);
16151619
const [todos, updateTodos] = useImmer(
16161620
initialTodos
16171621
);
16181622

16191623
function handleAddTodo(title) {
16201624
updateTodos(draft => {
16211625
draft.push({
1622-
id: nextId++,
1626+
id: nextId,
16231627
title: title,
16241628
done: false
16251629
});
16261630
});
1631+
setNextId(nextId + 1);
16271632
}
16281633

16291634
function handleChangeTodo(nextTodo) {
@@ -1792,26 +1797,27 @@ import { useImmer } from 'use-immer';
17921797
import AddTodo from './AddTodo.js';
17931798
import TaskList from './TaskList.js';
17941799

1795-
let nextId = 3;
17961800
const initialTodos = [
17971801
{ id: 0, title: 'Buy milk', done: true },
17981802
{ id: 1, title: 'Eat tacos', done: false },
17991803
{ id: 2, title: 'Brew tea', done: false },
18001804
];
18011805

18021806
export default function TaskApp() {
1807+
const [nextId, setNextId] = useState(3);
18031808
const [todos, updateTodos] = useImmer(
18041809
initialTodos
18051810
);
18061811

18071812
function handleAddTodo(title) {
18081813
updateTodos(draft => {
18091814
draft.push({
1810-
id: nextId++,
1815+
id: nextId,
18111816
title: title,
18121817
done: false
18131818
});
18141819
});
1820+
setNextId(nextId + 1);
18151821
}
18161822

18171823
function handleChangeTodo(nextTodo) {

0 commit comments

Comments
 (0)