Skip to content

Commit c47720a

Browse files
committed
Updated starter
1 parent 52cff05 commit c47720a

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

README.md

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,13 @@ function Toggle() {
166166
```
167167

168168
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:
170170

171171
```js
172-
<button style={{ border: "2px solid red" }}>
172+
<button style={{ background: "red" }}>
173173
```
174174

175-
When it's OFF, it should have a black border.
175+
When it's OFF, it should have a white background.
176176

177177
Let's go through those same questions to determine if we need to add state for this feature.
178178

@@ -194,10 +194,10 @@ function Toggle() {
194194
setIsOn((isOn) => !isOn);
195195
}
196196

197-
const color = isOn ? "red" : "black";
197+
const color = isOn ? "red" : "white";
198198

199199
return (
200-
<button style={{ border: `2px solid ${color}` }} onClick={handleClick}>
200+
<button style={{ background: color }} onClick={handleClick}>
201201
{isOn ? "ON" : "OFF"}
202202
</button>
203203
);
@@ -350,25 +350,11 @@ causing the number to be removed from the list.
350350
Here's a tough one! We've seen how to add and remove elements from arrays, but
351351
what about updating them?
352352

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.
355355

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.
372358

373359
Once again, there are a few approaches you could take here, so try to find a
374360
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:
393379
[1, 2, 3].map((number) => {
394380
if (number === 3) {
395381
// if the number is the one we're looking for, increment it
396-
return number + 1;
382+
return number + 100;
397383
} else {
398384
// otherwise, return the original number
399385
return number;
400386
}
401387
});
402-
// => [1,2,4]
388+
// => [1,2,103]
403389
```
404390

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
406392
handler would look:
407393

408394
```js
409-
function handleLiDoubleClick(numberToUpdate) {
395+
function handleLiClick(numberToUpdate) {
410396
const newNumberArray = numbers.map((number) => {
411397
if (number === numberToUpdate) {
412-
return numberToUpdate + 1;
398+
return numberToUpdate + 100;
413399
} else {
414400
return number;
415401
}
@@ -418,6 +404,19 @@ function handleLiDoubleClick(numberToUpdate) {
418404
}
419405
```
420406

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+
421420
### Array Cheat Sheet
422421

423422
Here's a quick reference of some common techniques for manipulating arrays in

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
3-
import Counter from "./components/Counter";
3+
import App from "./components/App";
44

5-
ReactDOM.render(<Counter />, document.getElementById("root"));
5+
ReactDOM.render(<App />, document.getElementById("root"));

0 commit comments

Comments
 (0)