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.
-
12:58
Cash Jordan
9 hours agoMexican MOB OVERTHROWS Capital... as "Socialist President" FLOODS AMERICA with CARTELS
32.7K14 -
23:13
Jasmin Laine
10 hours agoPBO Breaks His Silence—“This Is Soviet Stuff”… and the Panel EXPLODES
25.5K21 -
1:17:26
Jamie Kennedy
23 hours agoCatching Up With Deep Roy: JKX Stories, Star Wars Secrets, and Total Chaos | Ep 231 HTBITY
19.3K3 -
1:28:42
ThisIsDeLaCruz
5 hours ago $3.00 earnedThe Secrets Behind Madonna’s Legendary Live Sound
22.1K7 -
1:22:15
Glenn Greenwald
9 hours agoTrump and JD Vance Weigh in on the MAGA Civil War Over Tucker; Zelensky's Top Associates Embroiled in $100 Million Corruption Scandal; FBI's Ongoing Concealment About Trump Shooter | SYSTEM UPDATE #548
126K110 -
2:34:51
megimu32
5 hours agoON THE SUBJECT: 2000s Pop Punk & Emo Nostalgia — Why It Still Hits
22.6K6 -
3:44:13
VapinGamers
6 hours ago $1.68 earnedBattlefield RedSec - Getting Carried Maybe? I Need the Wins! - !rumbot !music
15.2K3 -
1:02:08
BonginoReport
9 hours agoThe Internet’s NSFW Reactions To “Bubba” Email - Nightly Scroll w/ Hayley Caronia (Ep.179)
76.7K62 -
2:36:48
XDDX_HiTower
5 hours ago $0.71 earnedARC RAIDERS, FIRST DROP IN
12.8K -
0:54
Gaming on Rumble
11 hours agoRumble Premium x Preplexity Pro Subscription Bundle
23.1K3