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.
-
2:16:09
DLDAfterDark
7 hours ago $4.77 earnedThe Very Merry HotDog Waffle Christmas Stream! Gun Talk - God, Guns, and Gear
39.7K8 -
1:19:51
Tundra Tactical
19 hours ago $16.82 earnedThursday Night Gun Fun!!! The Worlds Okayest Gun Show
63.2K -
55:11
Sarah Westall
1 day agoHumanity Unchained: The Awakening of the Divine Feminine & Masculine w/ Dr. Brianna Ladapo
50.3K5 -
1:42:41
Glenn Greenwald
13 hours agoReaction to Trump's Primetime Speech; Coldplay "Adultery" Couple Reappears for More Shame; Australia and the UK Obey Israel's Censorship Demands | SYSTEM UPDATE #560
167K105 -
2:46:41
Barry Cunningham
11 hours agoBREAKING NEWS: President Trump Signs The National Defense Authorization Act | More News!
68.8K34 -
43:10
Donald Trump Jr.
13 hours agoThe Days of Destructive DEI are Over, Plus Full News Coverage! | TRIGGERED Ep.301
127K103 -
52:07
BonginoReport
12 hours agoThe Internet Picks Bongino’s FBI Replacement - Nightly Scroll w/ Hayley Caronia (Ep.200)
119K98 -
55:30
Russell Brand
14 hours agoStay Free LIVE from AmFest — Turning Point USA - SF665
142K14 -
1:11:26
Kim Iversen
15 hours agoTrump Pulls War Fake-Out…Attack on Venezuela Still Coming!?
93.4K127 -
2:04:53
Redacted News
13 hours agoPutin just changed EVERYTHING with this move and NATO can't do anything | Redacted w Clayton Morris
184K159