Premium Only Content

React.js Box Component Color Controlled via Text Field
Continuing from the example of using the library React.js to build user interfaces. In this particular situation, a colored Box component whose color is controlled via a property. Now the goal is to change the color of a Box component using a text field.
Using the hook useState, you can keep track of another state variable to hold the color name. That value is tied to an input of attribute type having the value text. That input is placed next to the existing button to change the color.
You learn about controlled components, those whose value and onChange props are defined explicitly and controlled using a React state value. A function is defined to handle the changing of the text. That is, when the user types into the text field, the onChange handler function is called to update the value in state.
An event handler function has a parameter called event that includes details about who triggered the event. In particular, you can use the event target to access the input element so that you can retrieve the value that was typed by the user.
You can call on the mutate function for the color name to update its value after the user types into the text field. Then, the click handler for the button to change color is modified to set the Box component color prop to the value that was typed into the text field for the color name. That is, the click of the button takes the state value for color name and uses that as the prop passed to the Box component.
The lesson also briefly mentions how in the past React.js components used to be mostly written with classes instead of functions. But that paradigm has since been changed and writing functional components is more encouraged now. Questions about the order of JavaScript code statements are also addressed, like the definition of a function inside a function and the order the functions are defined.
-
5:17
Mrgunsngear
1 day ago $7.62 earnedPresident Trump Has Appointed A New ATF Director
42.8K37 -
48:17
Athlete & Artist Show
8 days ago $3.18 earnedS5E1: Chucky Announces First Kid, 4 Nations Face Off, and more!
54.2K3 -
38:30
hickok45
10 hours agoSunday Shoot-a-Round # 269
72.1K18 -
1:39:55
Squaring The Circle, A Randall Carlson Podcast
1 day ago#040 Humanity's Expansion Into The Cosmos: A New Age - Squaring The Circle
41.8K6 -
12:54
ariellescarcella
19 hours agoYou're NOT Queer, Just Annoying And Boring
29.3K21 -
18:57
Fit'n Fire
16 hours ago $1.57 earnedA PDW That Thumps -- Stribog SP45A3 45ACP
28.5K2 -
2:06:23
Game On!
20 hours ago $2.18 earnedAnother Sunday Without Football...
35.3K3 -
17:53
Forrest Galante
21 hours agoHow I Joined a Dangerous Remote Tribe (feat. Nelk Boys)
89K15 -
18:12:15
Vocalot
1 day agoDay 6! New Here! New Rumble Friends!? 🤙
103K9 -
34:27
The Connect: With Johnny Mitchell
1 day ago $18.79 earnedCan He Stop Them? Inside Trumps War On Mexican Drug Cartels & The New Era Of Mexican Organized Crime
56.5K27