diff --git a/src/components/Programmers.js b/src/components/Programmers.js
index bb4aee6bd..939318100 100644
--- a/src/components/Programmers.js
+++ b/src/components/Programmers.js
@@ -11,7 +11,7 @@ We can only feature one awesome programmer at a time.
Find comments below to help you along.
*/
-import React from 'react';
+import React, {useState} from 'react';
// Use this variable ONLY to initialize a slice of state!
// There is something in the JSX right now breaking this rule...
@@ -28,8 +28,16 @@ export const listOfAwesome = [
export default function Programmers() {
// We'll have to use the state hook twice, as we need two slices of state.
// The programmers list on the one hand, and the id of the featured programmer on the other.
-
+ const [programmers, setProgrammers] = useState(listOfAwesome)
+ const [featured, setFeatured]= useState(null)
const getNameOfFeatured = () => {
+
+ // for (let i = 0 ; i < programmers.length; i++){
+ // if(programmers.id === featured){
+ // return programmers[i].name
+ //
+ const foundDev = programmers.find(dev => dev.id ===featured)
+ return foundDev.name
// Leave this for last!
// This is NOT an event handler but a helper function. See its usage inside the JSX.
// It's going to utilize both slices of state to return the _name_ of the featured dev.
@@ -40,7 +48,7 @@ export default function Programmers() {
const style = {
fontSize: '1.5em',
marginTop: '0.5em',
- color: 'royalblue', // 🤔 color turns to gold, when celebrating
+ color: featured ? "gold" : 'royalblue', // 🤔 color turns to gold, when celebrating
};
return (
@@ -51,21 +59,21 @@ export default function Programmers() {
/* Nasty bug! We should map over a slice of state, instead of 'listOfAwesome'.
We might think: "it works, though!" But if the list of programmers is not state,
we could never add or edit programmers in the future. The list would be a static thing." */
- listOfAwesome.map(dev =>
+ programmers.map(dev =>
- {dev.name}
+ {dev.name}
)
}
{
+ featured
// Ternaries are fantastic to render "one thing or the other" depending on the "truthiness" of something.
// Pseudo-code: if the currently featured id is truthy render text 1, otherwise render text 2.
// Replace the hard-coded false with the correct variable.
- false
- ? `🎉 Let's celebrate ${getNameOfFeatured()}! 🥳`
- : 'Pick an awesome programmer'
+
+ ? `🎉 Let's celebrate ${getNameOfFeatured()}! 🥳` : "Pick an awesome programmer"
}
diff --git a/src/components/Spinner.js b/src/components/Spinner.js
index d0326fd34..d0d656d00 100644
--- a/src/components/Spinner.js
+++ b/src/components/Spinner.js
@@ -37,23 +37,24 @@ STEP 4:
Do you remember the operator we use to do "not"?
*/
-import React from 'react'; /* STEP 0 */
+import React,{useState} from 'react'; /* STEP 0 */
export default function Spinner() {
/* STEP 1 */
-
+const [spinnerOn, setSpinnerOn] = useState(true)
const toggleSpinner = () => {
/* STEP 4 */
+ {spinnerOn === true ? setSpinnerOn(false) : setSpinnerOn(true)}
};
return (
Spinner
{
- true &&
--+--
/* STEP 2 */
+ spinnerOn &&
--+--
/* STEP 2 */
}
);
diff --git a/src/components/Squares.js b/src/components/Squares.js
index 25ab72546..933610044 100644
--- a/src/components/Squares.js
+++ b/src/components/Squares.js
@@ -14,30 +14,35 @@ Only one square (or none) can be active at any given point.
Find comments below to help you along.
*/
-import React from 'react';
+import React , {useState} from 'react';
// Use this variable ONLY to initialize a slice of state!
const listOfSquareIds = ['sqA', 'sqB', 'sqC', 'sqD'];
export default function Squares() {
+const [squares, setSquares] = useState(listOfSquareIds)
+const [activeSquares, setActiveSquares] = useState(null)
// Use the state hook twice, as we need two slices of state: 'squares' and
// 'activeSquare'. One holds the _array_ of square ids, and the other keeps track
// of the currently active square. On page load there's no active square,
// so the value of 'activeSquare' should be null.
const getClassName = id => {
+
// This is NOT a click handler but a helper, used inside the JSX (see below).
// It should return a string containing the class name of 'active', if the id passed
// as the argument matches the active square in state, empty string otherwise.
// Right-click and "inspect element" on the square to see its effect.
- return ''
+ return id === activeSquares ? "active" : ""
};
const markActive = id => {
+
// This is a helper used inside an _inlined_ click handler (see below).
// Set the id argument to become the active id in state
// (unless it already is, in which case we should reset
// the currently active square id back to initial state).
+ id === activeSquares ? setActiveSquares(null) : setActiveSquares(id)
};
return (
@@ -48,7 +53,7 @@ export default function Squares() {
// Nasty bug! We should map over a slice of state, instead of 'listOfSquareIds'.
// We might say: "it works, though!" But if the list of squares is not state,
// we could never add squares, change squares or remove squares in the future. Fix!
- listOfSquareIds.map(id =>
+ squares.map(id =>