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.
-
1:58:31
Revenge of the Cis
3 hours agoEpisode 1432: Take Two
27.6K3 -
5:43:05
Bitcoin Magazine
7 days agoLIVE: Donald Trump Inauguration | America's First Bitcoin President
37.9K2 -
1:04:13
In The Litter Box w/ Jewels & Catturd
19 hours agoINAUGURATION DAY | In the Litter Box w/ Jewels & Catturd – Ep. 723 – 1/20/2025
43.2K19 -
3:20:22
Steven Crowder
9 hours agoCrowder Inauguration Day Live Stream 2025 | The Return of Donald Trump!
1.11M399 -
3:54:12
vivafrei
7 hours agoTHE DON OF A NEW AMERICA! Trump Inauguration Live Stream! Viva Frei Live!
109K70 -
3:42:54
The Quartering
1 day agoTrump Inauguration LIVE Broadcast & Commentary With DecoyVoice, Hannah Clare & Styxhexenhammer!
210K49 -
6:25:30
LFA TV
1 day agoLIVE: INAUGURATION OF PRESIDENT DONALD J. TRUMP
301K83 -
53:47
Russell Brand
7 hours agoInauguration Day Live! – SF522
197K98 -
2:43:30
Donald Trump Jr.
9 hours agoFull Coverage of My Father’s Inauguration, America is Back. | TRIGGERED Ep.209
356K302 -
1:49:47
Donald J. Trump
7 hours agoThe 60th Presidential Inauguration Ceremony
370K645