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.
-
59:56
X22 Report
22 hours agoMr & Mrs X - [DS] Trafficking Empire – The Pedo Network Island, The Cover-Up: Part 2 - Ep 6
103K38 -
1:03:07
Candace Show Podcast
2 days agoI’M BACK! And STILL Asking Questions (Sorry Brigitte). | Candace Ep 233
133K228 -
13:13
Mrgunsngear
21 hours ago $8.03 earnedStreamlight TLR-1 HP Review: Can It Dethrone Surefire?
59.8K20 -
1:26:34
Man in America
1 day agoExposing the Cover-Up That Could Collapse Big Medicine: Parasites
111K134 -
1:12:09
Wendy Bell Radio
10 hours agoPet Talk With The Pet Doc
38.9K52 -
27:15
Liberty Hangout
2 days agoThe Most DELUSIONAL Democrats on Earth!
60K173 -
38:41
JohnXSantos
1 day ago $1.45 earnedHow To Start A CLOTHING BRAND on a BUDGET! Step X Step (2025)
30.6K2 -
30:57
Her Patriot Voice
21 hours ago $17.71 earnedDemocrats More Unhinged Than EVER Before!
114K156 -
29:13
Clownfish TV
1 day agoGen Z are Becoming the Boomers?! | Clownfish TV
33.9K41 -
1:48:31
Squaring The Circle, A Randall Carlson Podcast
1 day agoMEGA Tsunamis and the formation of our World ft. Dr. Dallas Abbot
45.8K10