You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don't know if I'm breaking some convention here but I've
previously received a rule of thumb that it's better to not use
negation if it's not strictly necessary.
setNotes(notes.map(n=>n.id!== id ?n :response.data))
207
+
setNotes(notes.map(n=>n.id=== id ?response.data: n))
208
208
})
209
209
}
210
210
```
@@ -243,17 +243,17 @@ The callback function sets the component's <em>notes</em> state to a new array t
243
243
244
244
```js
245
245
axios.put(url, changedNote).then(response=> {
246
-
setNotes(notes.map(note=>note.id!== id ?note :response.data))
246
+
setNotes(notes.map(note=>note.id=== id ?response.data: note))
247
247
})
248
248
```
249
249
250
250
This is accomplished with the <em>map</em> method:
251
251
252
252
```js
253
-
notes.map(note=>note.id!== id ?note :response.data)
253
+
notes.map(note=>note.id=== id ?response.data: note)
254
254
```
255
255
256
-
The map method creates a new array by mapping every item from the old array into an item in the new array. In our example, the new array is created conditionally so that if <em>note.id !== id</em> is true; we simply copy the item from the old array into the new array. If the condition is false, then the note object returned by the server is added to the array instead.
256
+
The map method creates a new array by mapping every item from the old array into an item in the new array. In our example, the new array is created conditionally so that if <em>note.id === id</em> is true; the note object returned by the server is added to the array. If the condition is false, then we simply copy the item from the old array into the new array instead.
257
257
258
258
This <em>map</em> trick may seem a bit strange at first, but it's worth spending some time wrapping your head around it. We will be using this method many times throughout the course.
259
259
@@ -320,7 +320,7 @@ const App = () => {
320
320
noteService
321
321
.update(id, changedNote)
322
322
.then(response=> {
323
-
setNotes(notes.map(note=>note.id!== id ?note :response.data))
323
+
setNotes(notes.map(note=>note.id=== id ?response.data: note))
324
324
})
325
325
// highlight-end
326
326
}
@@ -448,7 +448,7 @@ const App = () => {
448
448
.update(id, changedNote)
449
449
// highlight-start
450
450
.then(returnedNote=> {
451
-
setNotes(notes.map(note=>note.id!== id ?note:returnedNote))
451
+
setNotes(notes.map(note=>note.id=== id ?returnedNote:note))
452
452
// highlight-end
453
453
})
454
454
}
@@ -665,7 +665,7 @@ const toggleImportanceOf = id => {
665
665
666
666
noteService
667
667
.update(id, changedNote).then(returnedNote=> {
668
-
setNotes(notes.map(note=>note.id!== id ?note:returnedNote))
668
+
setNotes(notes.map(note=>note.id=== id ?returnedNote:note))
0 commit comments