In the lesson earlier you:
-
Learned how to use React Router to perform client-side routing.
-
Refactored the react-mastermind app to render a
<GamePage>component at the root route. -
Added a "Difficulty"
<Link>to the<GamePage>used to navigate to a<SettingsPage>component. -
As a practice exercise, added an additional
<Route>with a path of/settings. -
Created a minimal
<SettingsPage>component that included a "HOME"<Link>.
In this lab, you'll continue to have fun building out react-mastermind using what you know about components, state, props, styling, methods, event handlers, routing and of course, JavaScript.
The starter code for this lab is the same as the completed code from the React Router lesson.
To get set up for this lesson:
cdinto the folder for this lab.- Install the Node modules:
$ npm i - Open the project in VS Code:
$ code . - Open a terminal in VS Code (
ctrl + backtick) - Start the React Dev Server:
$ npm start
After the server starts up, you should see the following in the browser at localhost:3000:
In this lab, you'll be adding the "Difficulty" setting functionality by completing the exercises below.
When completed, clicking the "Difficulty" link (styled as a button) will display the following:
As you can see, the settings page allows the player to change the difficulty level by selecting the number of colors available to choose from!
-
Currently, the
<GamePage>component is relying on CSS classes defined in App.css. Refactor to cure this inappropriateness by copying the classes in App.css over to GamePage.css created during the lesson. Update the class names and update GamePage.jsx as required to use those class names. -
Since both
<App>&<GamePage>rely on a*-header-footerclass with the same styling, refactor by renaming it toheader-footerand putting it in index.css instead. Update App.js & GamePage.jsx to useheader-footer, then you can deleteGamePage-header-footerfrom GamePage.css and all of the CSS in App.css. -
There will be three levels of difficulty: 'Easy'; 'Moderate'; and 'Difficult'. The game's difficulty will be held in a state property named
difficulty. Add thedifficultyproperty tostateand initialize it with a value of'Easy'. However,difficultyshould not be "reset" if the player clicks the [New Game] button.Hint: Not resetting
difficultyrequires that it not be part of the object returned by thegetInitialStatemethod. Instead,difficultyshould be initialized one time on thestatestate object within theconstructor. -
Using strings such as 'Easy', etc., to represent the
difficultyis a fantastic way to access the array of colors for a particular difficulty level by using an object as a lookup. Refactor thecolorsarray in App.js to be an object with keys ofEasy,'ModerateandDifficultwhich hold arrays of 4, 5, or 6 color strings respectively.Hint: The first couple of lines will look like this
const colors = { Easy: ['#7CCCE5', '#FDE47F', '#E04644', '#B576AD'], ...
-
Changing the structure of
colorsexpectedly broke the code because we were used to passingcolorsas an array to<GamePage>. Refactor the value that is assigned to thecolorsprop in<GamePage>such that the appropriate array in the refactoredcolorsobject is passed according to the value of thedifficultystate property. With this step complete, the react-mastermind will be working again. -
Now comes the "fun" part - building out the
<SettingsPage>component so that:-
It displays the UI shown above, including the three difficulty levels, with a button to select the level and the colors rendered as pegs. Also, theres a "Cancel" link used to return to the root without changing the difficulty.
-
The button to select the difficulty level is disabled for the currently selected difficulty.
-
Clicking one of difficulty buttons should update the
difficultystate, initialize a new game, and programmatically route back to the<GamePage>page (root route).
Hints:
-
You can try downloading and using React Developer Tools to browse components and check/modify state & props.
-
<SettingsPage>is going to need both thecolorsobject and thedifficultystate property. -
Since
difficultylives in the state of<App>, guess where the method to update it will live. -
You can reuse the
handleNewGameClickmethod by passing it down to<SettingsPage>calling it to reset the game after you call the method to update thedifficulty. -
After updating
difficulty& invokinghandleNewGameClick, you can use the technique shown in the React Router lesson to programmatically route to/.
-
Choosing the Difficult level will result in the root route displaying this:
Good luck cracking the code!



