Create a new React project in stackblitz.com
-
Remove everything other than the
<div>
from<App>
-
Above
export default function App() {
, add the following array:const colors = ["orange", "purple"];
-
Use
map
to render a<button>
for each color string in thecolors
array. Assign akey
prop to prevent warnings. Do not render any text inside of the button. -
Add the following CSS to App.css:
.App > button { height: 5rem; width: 5rem; border-radius: 50%; margin: 1rem; }
-
Use the
style
prop on the<button>
to set thebackgroundColor
to equal that of the color string. Hint: Thestyle
prop must be assigned an object with a property for each CSS property we want styled, where the key is the name of the CSS property. -
Add a
selectedColorIdx
state variable and initialize it to the value of0
('orange'). HINT: Don't forget to importuseState
and name the setter function conventionally. -
Add
onClick
to the<button>
and write the code that will updateselectedColorIdx
to the index of the button/color. HINT: You'll need to ensure that themap
method's callback function has a second parameter defined to accept the index of the iteration. Also, you're calling the setter function with an argument, so... -
Use React DevTools to verify that the
selectedColorIdx
state is being properly updated. -
Add an empty
<div></div>
below the buttons. -
Add the following CSS to App.css:
.App > div { height: 10rem; width: 10rem; border: 0.5rem solid black; }
-
Add a
style
prop that will setbackgroundColor
of the<div>
to that of the selected color. HINT: You'll need to use thecolors
array.