Exercises
Controlled Country Picker
The “Big List O’ Countries” select is a staple of e-commerce sites. Let's build one!
Using the data provided in the COUNTRIES object, create a <select> tag with options for every country. Bind this <select> tag to the provided country state variable.
Acceptance Criteria:
- Use the
COUNTRIESconstant to dynamically generate the set of<option>elements.- In order to map over an object, you'll need to use something like Object.keys() or Object.entries()
- There should be a "blank" option, selected by default. It shouldn't default to the first country in the list.
- The indicator at the bottom should update when the user changes their selected country.
- No warnings in the dev console
Code Playground
Solution:
Two-Factor Authentication
Two-factor authentication has quickly become a best practice in terms of securing logins for highly-sensitive accounts. The most common form I've seen is that the user is prompted to enter a short code, generated from an app.
In this exercise, we'll build this form!
Acceptance Criteria:
- The input's value should be held in React state.
- When the user submits their code, a
window.alertshould let them know whether it's correct or not, by comparing their submitted value with theCORRECT_CODEconstant. - A
<form>tag should be used.
Code Playground
Solution:
Generative Art
In this exercise, we're building a tool to produce generative art!
The tool is nearly complete, but the form controls need to be wired up. Your job is to connect the React state to the form controls, so that tweaking the controls will update the art.
Acceptance Criteria:
- The range slider should be bound to the
numOfLinesstate. - The select control should be bound to the
colorThemestate. - The radio buttons should be bound to the
shapestate. - The radio button labels should work correctly. The user should be able to click the text "Polygons" to select that option.
- The inputs should conform to HTML standards (eg. radio buttons should be grouped using the “name” attribute).
Note: All of your changes should happen in App.js. The other files are shown in case you're curious how it works, but you can safely ignore them and focus exclusively on App.js.
Code Playground
Solution:
Video Summary
- We can wire up our
numOfLinesslider as if it was a text input, settingvalueandonChange - Similarly, the
colorModeselect can be wired up by passingvalueandonChangeto the parent<select>tag (not touching the options) - The radio buttons require a bit of fixing, before we can get to the React state stuff:
- We need to add a
nameso that the two radio buttons function as a group, so that only one can be selected at a time - We need to add an
idand anhtmlForso that the label functions properly
- To wire them up, we need to add a
valueprop, but unlike with<select>and<input>,valueisn't the React state-binding attribute. Instead,valuespecifies what each option should hold.- Think of it like how the
valueproperty works on<option>inside a<select>.
- To start binding to React state, we need to add a
checkedproperty to each one. This property is a boolean value, depending on whether the current option should be selected or not. - Finally, we add an
onChangehandler. This works very similar to the other inputs we've seen. - Don't neglect the “value” prop! While it can be skipped by specifying a string in the
onChangehandler, this is not recommended, as it deviates from DOM specifications, and affects the ability for this form to work without React.