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.
-
52:56
X22 Report
7 hours agoMr & Mrs X - Halftime Narrative Failed, Epstein Is A Puppet, Trump Is Saving America - EP 26
130K33 -
24:09
The Rubin Report
8 hours agoHow Dems Created an ICE Recipe for Disaster on Purpose | Anna Paulina Luna
58.9K54 -
20:02
WhatCulture - Film
4 days ago $3.88 earned20 Movie Reveals Nobody Noticed
36.6K2 -
6:49
RidiculousRides
1 day ago $2.89 earnedPeel P50: The 59kg Car You Can Park Indoors
44K5 -
13:02
WhatCulture Gaming
2 days ago $1.58 earned10 Video Games That Broke The Rules
32.8K -
2:12:02
Battlefront: Frontline with Dustin Faulkner
17 hours agoThe DoJ Leadership Reveals Failure to the People with Epstein Files During Bondi Testimony | Has the GOP Lost the Midterms with Bad Focus | Trust in Media and Government Definitely Failing in the West
37.7K11 -
7:58
CinemaBlend
2 days ago $1.11 earned'The Penguin' Interviews With Colin Farrell, Cristin Milioti, Matt Reeves And More
34.9K1 -
22:33
Welker Farms
1 day ago $9.80 earned$4,000 Leak? Let's Crack It Open & Investigate!
94K11 -
20:42
Jasmin Laine
21 hours agoSupreme Court CANCELS Election Win—Liberal MP LOSES It After VIRAL Clip HAUNTS Him
60.7K86 -
15:03
tactical_rifleman
1 day ago $8.14 earnedGriffin Armament MK2 Review
39.2K10