Build a Color Picker App - Build a Color Picker App

Tell us what’s happening:

Please help Im not sure why export component is not being recognized or useState Hook. The

is not passing either. can I please get a hint?

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 */
import { useState } = React;

export const ColorPicker= () =>{
  const [color, setColor] = useState('#ffffff');
   return(
<div className = "color-picker-container" style={{ backgroundColor = "white"}} >
<p>Pick A Color</p>
<input className="color-input" type="color"/>
</div>
   )
};

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36

Challenge Information:

Build a Color Picker App - Build a Color Picker App
https://www.freecodecamp.org/learn/full-stack-developer/lab-color-picker/build-a-color-picker-app

Hi @aliciadorsey2

This syntax does not look right.

Happy coding

1 Like

Tell us what’s happening:

Please help, can I get a hint to why my #color-input is not reading my useState?

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 [color, setColor] = useState('hex','#ffffff');
   return(
    
<div id = "color-picker-container" style={{ backgroundColor:"white" }} >
   
 
      <p>"Choose a Color"</p>
<input id ="color-input" type="color" value="useState" color={color}  onChange = {setColor}/>
</div>
   )
};

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36

Challenge Information:

Build a Color Picker App - Build a Color Picker App
https://www.freecodecamp.org/learn/full-stack-developer/lab-color-picker/build-a-color-picker-app

look at the difference between these two
and also you are not actually getting the state with that

I have merged your two topics, please do not open multiple topics for the same challenge

hi I appreciate your response but I’m totally lost on this lab. can you please let me know why #color-input is not reading my useState?

I figured out, thanks!!

hi I’m stuck again and don’t know how to change the background when the color changes. please help!!

you should probably post your newest code in your reply.
(if you don’t know how, let us know…)

ps. make sure you understand how useState works.
It needs to be given a function to call when the state changes. The function has to be defined in your code. (You can refer to the React documentation if you need to understand more https://react.dev/learn/state-a-components-memory )

hi, I’m so new to React and so lost with creating this final step. I created several functions and nothing is working. can you please give me a hint on how to target the background color in my input. I thought that was my target. :sleepy_face:

const { useState } = React;

export const ColorPicker = () => {
  const [color, setColor] = useState('#ffffff');
  
    
  

   return(
<div id = "color-picker-container"  style={{ backgroundColor:"white" }} >
   
 
      <p>"Choose a Color"</p>

<input id ="color-input" type="color" value={color} color={color}  onChange={setColor}/>

</div>
   )
};

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

so when the color changes, you’re expecting React to run which code? (be specific please when you respond)

that’s where I’m stuck, I don’t know how to create a function that targets the input background.

I’m just going to take a break because I’m in tears right now and feel like I can just do some research and come back to this issue.

okay, look at this code:

<div id = "color-picker-container"  style={{ backgroundColor:"white" }} >

What does this code do?
How do we make it interactive so it doesn’t always show a white background?

the other thing to consider what you’re doing for the onChange part of the input element.
Can you find an example from something you’ve learned in a lecture on what should go there?
(Right now, what does your code do onChange ? Try to analyze it step by step and explain it to yourself - then to us - so we can fix your understanding)

You may try this out
mod edit: code removed

please don’t give people code that you’ve written in response to their questions.

okay. thanks for that.

Your function does not have to target the input background, it just has to properly call the setColor. This changes your color variable you get from useState and in consequence, it changes the input background. However, for the background color change your “backgroundColor” in the “style” attribute of your “color-picker-container” element must be equal to the color variable.

The idea is that the background color depends on the variable color you get from useState, and then, you change that color through the setColor (also gotten from useState). This setColor is called in a function that triggers when the “onChange” attribute of your input element changes (that is, when you pick a color in the color picker).

I recommend you to check the workshop previous to this lab so you can see an example of that.

So, in summary:

  1. You need to create a function that uses setColor, so the color variable changes (this function’s name might start with “handle”, for example, “handlePickColor”. This you can also see in that workshop. The function must be defined inside “export const ColorPicker”).
  2. The onChange attribute in your input element must be equal to that function, so it calls it when its value changes.
  3. The backgroundColor in the style attribute in your “color-picker-container” must be equal to color, so when color changes, this background changes too.

this is a project in the curriculum, it requires specific requirements to be met