omgovich / react-colorful

🎨 A tiny (2,8 KB) color picker component for React and Preact apps
https://omgovich.github.io/react-colorful
MIT License
3.19k stars 100 forks source link

Color pointer not resetting after color change #193

Open grantspilsbury opened 2 years ago

grantspilsbury commented 2 years ago

I am trying to reset the color pointer button after I click a button but the pointer remains the previously selected color.

In the example below I expect the color pointer to change to white (not green, and be in the correct position) after the Set button is clicked.

const Panel = () => {
  const [color, setColor] = useState('white')
  const handleColorChange = (color) => setColor(color)

  return (
    <>
      <HexColorPicker color={color} onChange={handleColorChange} />
      <button onClick={() => setColor('white')}>
          Set
      </button>
    </>
  )
}
export default Panel

What am I doing wrong?

Screen Shot 2022-09-13 at 9 00 51 am

GabrielModog commented 2 years ago

Hi Grant! Are you good? Hope you going well!

This can be solved by adding hex color instead of the color name. You are importing a component that handles with hex color only.

const Panel = () => {
  const [color, setColor] = useState("#ffffff");
  const handleColorChange = (color) => setColor(color)

  return (
    <>
      <HexColorPicker color={color} onChange={handleColorChange} />
       <button onClick={() => setColor("#ffffff")}>Reset</button>
    </>
  )
}
export default Panel