@@ -166,13 +166,13 @@ function Toggle() {
166
166
```
167
167
168
168
With this state variable in place, let's add another feature to our button. When
169
- the button is ON, let's add a red border around the button , like this:
169
+ the button is ON, let's make the background red , like this:
170
170
171
171
``` js
172
- < button style= {{ border : " 2px solid red" }}>
172
+ < button style= {{ background : " red" }}>
173
173
```
174
174
175
- When it's OFF, it should have a black border .
175
+ When it's OFF, it should have a white background .
176
176
177
177
Let's go through those same questions to determine if we need to add state for this feature.
178
178
@@ -194,10 +194,10 @@ function Toggle() {
194
194
setIsOn ((isOn ) => ! isOn);
195
195
}
196
196
197
- const color = isOn ? " red" : " black " ;
197
+ const color = isOn ? " red" : " white " ;
198
198
199
199
return (
200
- < button style= {{ border : ` 2px solid ${ color} ` }} onClick= {handleClick}>
200
+ < button style= {{ background : color }} onClick= {handleClick}>
201
201
{isOn ? " ON" : " OFF" }
202
202
< / button>
203
203
);
@@ -350,25 +350,11 @@ causing the number to be removed from the list.
350
350
Here's a tough one! We've seen how to add and remove elements from arrays, but
351
351
what about updating them?
352
352
353
- Let's add a feature so that when a user _ double clicks _ on a number, that number
354
- is incremented by 1 .
353
+ Let's update our click feature so that when a user clicks on a number, that
354
+ number is incremented by 100 .
355
355
356
- Here's how we can handle double clicks:
357
-
358
- ``` js
359
- const numberList = numbers .map ((num ) => (
360
- < li
361
- key= {num}
362
- onClick= {() => handleLiClick (num)}
363
- onDoubleClick= {() => handleLiDoubleClick (num)}
364
- >
365
- {num}
366
- < / li>
367
- ));
368
- ```
369
-
370
- Next, in the ` handleLiDoubleClick ` function, we need to figure out a way to
371
- update our array in state and increment the number that was double-clicked.
356
+ In the ` handleLiClick ` function, we need to figure out a way to
357
+ update our array in state and increment the number that was clicked.
372
358
373
359
Once again, there are a few approaches you could take here, so try to find a
374
360
solution on your own before peeking at the answer! Remember, we want to find a
@@ -393,23 +379,23 @@ Here's an example of using `.map` to update _one element_ of an array:
393
379
[1 , 2 , 3 ].map ((number ) => {
394
380
if (number === 3 ) {
395
381
// if the number is the one we're looking for, increment it
396
- return number + 1 ;
382
+ return number + 100 ;
397
383
} else {
398
384
// otherwise, return the original number
399
385
return number;
400
386
}
401
387
});
402
- // => [1,2,4 ]
388
+ // => [1,2,103 ]
403
389
```
404
390
405
- So to use that technique to solve our problem, here's how our double click event
391
+ So to use that technique to solve our problem, here's how our click event
406
392
handler would look:
407
393
408
394
``` js
409
- function handleLiDoubleClick (numberToUpdate ) {
395
+ function handleLiClick (numberToUpdate ) {
410
396
const newNumberArray = numbers .map ((number ) => {
411
397
if (number === numberToUpdate) {
412
- return numberToUpdate + 1 ;
398
+ return numberToUpdate + 100 ;
413
399
} else {
414
400
return number;
415
401
}
@@ -418,6 +404,19 @@ function handleLiDoubleClick(numberToUpdate) {
418
404
}
419
405
```
420
406
407
+ We can shorten this up a bit by using the ternary operator, and implicit return:
408
+
409
+ ``` js
410
+ function handleLiClick (numberToUpdate ) {
411
+ const newNumberArray = numbers .map ((number ) =>
412
+ number === numberToUpdate ? numberToUpdate + 100 : number
413
+ );
414
+ setNumbers (newNumberArray);
415
+ }
416
+ ```
417
+
418
+ (It's up to you which version of this syntax you find more legible!)
419
+
421
420
### Array Cheat Sheet
422
421
423
422
Here's a quick reference of some common techniques for manipulating arrays in
0 commit comments