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.
-
4:31
CoachTY
1 day ago $27.59 earnedCOINBASE AND DESCI !!!!
147K11 -
10:02
MichaelBisping
23 hours agoBISPING: "Was FURY ROBBED?!" | Oleksandr Usyk vs Tyson Fury 2 INSTANT REACTION
76.8K13 -
8:08
Guns & Gadgets 2nd Amendment News
2 days ago16 States Join Forces To Sue Firearm Manufacturers Out of Business - 1st Target = GLOCK
104K87 -
10:17
Dermatologist Dr. Dustin Portela
2 days ago $18.26 earnedOlay Cleansing Melts: Dermatologist's Honest Review
143K14 -
1:02:20
Trumpet Daily
2 days ago $44.35 earnedObama’s Fake World Comes Crashing Down - Trumpet Daily | Dec. 20, 2024
99.9K67 -
6:29
BIG NEM
1 day agoCultivating God Mode: Ancient Taoist NoFap Practices
73.5K20 -
30:53
Uncommon Sense In Current Times
2 days ago $11.44 earned"Pardon or Peril? How Biden’s Clemency Actions Could Backfire"
86.6K7 -
40:01
CarlCrusher
1 day agoSkinwalker Encounters in the Haunted Canyons of Magic Mesa - ep 4
79.7K10 -
59:44
PMG
2 days ago $10.65 earned"BETRAYAL - Johnson's New Spending Bill EXPANDS COVID Plandemic Powers"
80.3K51 -
6:48:50
Akademiks
1 day agoKendrick Lamar and SZA disses Drake and BIG AK? HOLD UP! Diddy, Durk, JayZ update. Travis Hunter RUN
198K35