From 28fffdb06eaf9addb00b0d521145d2f91d839136 Mon Sep 17 00:00:00 2001 From: Kevin Lai Date: Mon, 21 Mar 2022 18:22:40 -0400 Subject: [PATCH] finished mvps --- src/components/Programmers.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/components/Programmers.js b/src/components/Programmers.js index e34c4f392..f0d734196 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... @@ -27,6 +27,8 @@ 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 = () => { // Leave this for last! @@ -34,12 +36,17 @@ export default function Programmers() { // It's going to utilize both slices of state to return the _name_ of the featured dev. // The beauty of closures is that we can "see" both slices of state from this region // of the program, without needing to inject the information through arguments. + for(let i = 0; i < programmers.length; i++){ + if (programmers[i].id === featured) { + return programmers[i].name; + } + } }; const style = { fontSize: '1.5em', marginTop: '0.5em', - color: 'royalblue', // 🤔 color turns to gold, when celebrating + color: featured ? 'gold' : 'royal blue', // 🤔 color turns to gold, when celebrating }; return ( @@ -50,9 +57,9 @@ 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}
) } @@ -62,7 +69,7 @@ export default function Programmers() { // 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 + featured ? `🎉 Let's celebrate ${getNameOfFeatured()}! 🥳` : 'Pick an awesome programmer' }