diff --git a/README.md b/README.md index fd881bf9b..a138ac456 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,13 @@ This project includes a `src/components` folder containing several React compone ### Task 1: Project Set Up -- [ ] Create a forked copy of this project. -- [ ] Clone your OWN fork of the repository using your terminal. -- [ ] CD into the project base directory. -- [ ] Download project dependencies by running `npm install`. -- [ ] Start up the app using `npm start`. -- [ ] Optionally run tests using `npm test`. (The app must be running on `http://localhost:1234`) -- [ ] Push commits: `git push origin main`. +- [x] Create a forked copy of this project. +- [x] Clone your OWN fork of the repository using your terminal. +- [x] CD into the project base directory. +- [x] Download project dependencies by running `npm install`. +- [x] Start up the app using `npm start`. +- [x] Optionally run tests using `npm test`. (The app must be running on `http://localhost:1234`) +- [x] Push commits: `git push origin main`. ### Task 2a: Minimum Viable Product @@ -34,9 +34,9 @@ This project includes a `src/components` folder containing several React compone #### Steps -- [ ] You will add functionality to all components inside inside `src/components`. -- [ ] Work on the components **in the same order in which they display in Chrome** (to go from easiest challenge to hardest). -- [ ] Each file includes a link to a video, and a set of instructions which can be summarized as: +- [x] You will add functionality to all components inside inside `src/components`. +- [x] Work on the components **in the same order in which they display in Chrome** (to go from easiest challenge to hardest). +- [x] Each file includes a link to a video, and a set of instructions which can be summarized as: - Watch the video demoing the finished component, and think about how much state is needed. - Create the necessary slices of component state using the state hook. - Fix the JSX so it displays information derived from state, instead of hard-coded data. @@ -62,4 +62,4 @@ The move by the computer should probably be random if the previous checks turn o ## Submission Format -- [ ] Submit a link to your repo in canvas. +- [x] Submit a link to your repo in canvas. diff --git a/cypress.json b/cypress.json index acb2d0b3d..32a00b133 100644 --- a/cypress.json +++ b/cypress.json @@ -1,5 +1,6 @@ { "viewportWidth": 800, "viewportHeight": 1200, - "baseUrl": "http://localhost:1234" + "baseUrl": "http://localhost:1234", + "projectId": "z8ccb8" } diff --git a/src/components/Counter.js b/src/components/Counter.js index 447a5e849..9b44453a8 100644 --- a/src/components/Counter.js +++ b/src/components/Counter.js @@ -46,32 +46,37 @@ STEP 6: This click handler needs to use 'setCount' to set the 'count' to be zero again. */ -import React from 'react'; /* STEP 0 */ +import React, { useState } from 'react'; export default function Counter() { /* STEP 1 */ +const [count, setCount] = useState(0); const increment = () => { /* STEP 4 */ + setCount(count + 1) + }; const decrement = () => { /* STEP 5 */ + setCount(count - 1) }; const reset = () => { /* STEP 6 */ + setCount(0) }; const style = { fontSize: '1.5em', marginBottom: '0.3em', - color: 'royalblue', /* STEP 2 */ + color: count % 2 === 0 ? 'royalblue' : 'crimson' }; return (

Counter

- Number 0 is even {/* STEP 3 */} + Number {count} is {count % 2 === 0 ? 'even' : 'odd'}
diff --git a/src/components/Input.js b/src/components/Input.js index 36bf8fe03..3c941ee09 100644 --- a/src/components/Input.js +++ b/src/components/Input.js @@ -34,34 +34,37 @@ STEP 6: We need to add an extra prop to the element like so: value={inputValue} */ -import React from 'react'; /* STEP 0 */ +import React, { useState } from 'react'; export default function Input() { /* STEP 1 */ + const [inputValue, setInputValue] = useState(''); const changeInput = evt => { // When the input changes, its whole value can be found inside the event object. // Log out the synthetic event object 'evt' and see for yourself. const { value } = evt.target; + setInputValue(value) /* STEP 4 */ }; const reset = () => { + setInputValue('') /* STEP 5 */ }; const style = { fontSize: '1.5em', marginBottom: '0.3em', - color: 'royalblue', /* STEP 2 */ + color: inputValue.length >10 ? 'crimson' : 'royalblue', }; return (

Input

-
{/* STEP 3 */} +
{inputValue.toUpperCase()}
{/* STEP 3 */}
- {/* STEP 6 */} + {/* STEP 6 */}
diff --git a/src/components/Moods.js b/src/components/Moods.js index 98b49467f..a26ce2a0d 100644 --- a/src/components/Moods.js +++ b/src/components/Moods.js @@ -28,7 +28,7 @@ STEPS 4, 5, 6: Inside these click handlers set the correct mood, using 'setMood' and the variables below the imports. */ -import React from 'react'; /* STEP 0 */ +import React, { useState } from 'react'; const initialMood = 'Not sure how I feel'; const happyMood = 'Quite happy!'; @@ -36,27 +36,31 @@ const sadMood = 'Rather sad'; export default function Moods() { /* STEP 1 */ +const [mood, setMood] = useState(happyMood); const makeHappy = () => { + setMood(happyMood); /* STEP 4 */ }; const makeSad = () => { + setMood(sadMood); /* STEP 5 */ }; const reset = () => { + setMood(initialMood); /* STEP 6 */ }; const style = { fontSize: '1.5em', marginBottom: '0.3em', - color: 'crimson', /* STEP 2 */ + color: mood === happyMood? 'royalblye' : 'crimson', }; return (

Moods

-
Not sure how I feel
{/* STEP 3 */} +
{mood}
{/* STEP 3 */}
diff --git a/src/components/Programmers.js b/src/components/Programmers.js index bb4aee6bd..7eabc7cd4 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,6 +28,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! @@ -35,12 +37,14 @@ 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. + const foundDev = programmers.find(dev => dev.id === featured); + return foundDev.name; }; const style = { fontSize: '1.5em', marginTop: '0.5em', - color: 'royalblue', // 🤔 color turns to gold, when celebrating + color: featured ? 'gold' : 'royalblue', }; return ( @@ -51,9 +55,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}
) } @@ -63,7 +67,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' } diff --git a/src/components/Spinner.js b/src/components/Spinner.js index d0326fd34..5d9465631 100644 --- a/src/components/Spinner.js +++ b/src/components/Spinner.js @@ -36,13 +36,17 @@ STEP 4: This click handler needs to toggle the spinner by setting "whether on" to be the opposite of what it currently is. Do you remember the operator we use to do "not"? */ - -import React from 'react'; /* STEP 0 */ +import React, { useState } from 'react'; export default function Spinner() { + +const [spinnerOn, setSpinnerOn] = useState(true); + /* STEP 1 */ const toggleSpinner = () => { + + setSpinnerOn(!spinnerOn) /* STEP 4 */ }; @@ -50,10 +54,11 @@ export default function Spinner() {

Spinner

{ - true &&
--+--
/* STEP 2 */ + spinnerOn &&
--+--
/* STEP 2 */ } +
); diff --git a/src/components/Squares.js b/src/components/Squares.js index 25ab72546..bcaf4c0a2 100644 --- a/src/components/Squares.js +++ b/src/components/Squares.js @@ -14,7 +14,7 @@ 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']; @@ -24,13 +24,15 @@ export default function Squares() { // '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 [squares, setSquares] = useState(listOfSquareIds); + const [activeSquare, setActiveSquare] = useState(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 === activeSquare ? 'active' : '' }; const markActive = id => { @@ -38,7 +40,12 @@ export default function Squares() { // 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). - }; + if ( id === activeSquare ) { + setActiveSquare(null) + }else { + setActiveSquare(id) + } + } return (
@@ -48,7 +55,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 =>