Tell us what’s happening:
I am working with the useState hook in react and I am trying to apply the current value or color chosen in the input field to the color-picker-container’s background color. However, I am unsure how I should be applying that color with the handle function.
This is what I attempted originally for the return statement:
return (
<div style="background-color: {bgColor}" id="color-picker-container">
<input type="color" id="color-input" />
</div>
);
However, I can’t simply use it as a prop in this instance since it’s a string. I am unsure how I can apply a style to an HTML element when dealing with a useState hook.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Color Picker</title>
<link rel="stylesheet" href="styles.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.3.1/umd/react.development.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.3.1/umd/react-dom.development.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/7.26.5/babel.min.js"></script>
<script
data-plugins="transform-modules-umd"
type="text/babel"
src="index.jsx"
></script>
</head>
<body>
<div id="root"></div>
<script
data-plugins="transform-modules-umd"
type="text/babel"
data-presets="react"
data-type="module"
>
import { ColorPicker } from './index.jsx';
ReactDOM.createRoot(document.getElementById('root')).render(<ColorPicker />);
</script>
</body>
</html>
/* file: styles.css */
body,
html {
margin: 0;
padding: 0;
height: 100%;
font-family: Arial, sans-serif;
}
#color-picker-container {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
background-color: #ffffff;
}
input[type="color"] {
position: absolute;
margin-top: 50px;
height: 40px;
}
/* file: index.jsx */
const { useState } = React;
export const ColorPicker = () => {
const [bgColor, setBGColor] = useState();
const handleBackgroundColr = () => {
setBGColor(bgColor = input.value);
}
return (
<div id="color-picker-container">
<input type="color" id="color-input" />
</div>
);
};
Challenge Information:
Build a Color Picker App - Build a Color Picker App