casesandberg / react-color

:art: Color Pickers from Sketch, Photoshop, Chrome, Github, Twitter & more

Home Page:http://casesandberg.github.io/react-color/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

There might be wrong usage in the `examples/basic-with-react-hooks`, that will cause the <Alpha/> component not working.

xiaoyuze88 opened this issue · comments

When I try it out the demo using SketchPicker, I found out that the Alpha handle is not working.

After digging for a while I found the usage from the demo is wrong.

// In the demo
const [color, setColor] = useState();
  const handleChange = color => setColor(color);
  return (
    <div className="App">
      <SketchPicker color={color} onChangeComplete={handleChange} />
    </div>
  );

WeChat325b7dd1d9b44f7c3884da1396911acd

WeChat8d20778a8b3a3c2a70504a42fe1e4bda

After adding some logs in the source code(helpers/color.js -> toState()), you can see the onChange callback pass a color object to the setColor, which includes a param named hex, and that will cause the toState function to handle it as a pure hex, so the alpha is lost.

It should be used like this:

// In the demo
const [color, setColor] = useState();
  const handleChange = color => setColor(color.rgb);
  return (
    <div className="App">
      <SketchPicker color={color} onChangeComplete={handleChange} />
    </div>
  );