React-color: Can not pick a color.

Created on 14 May 2020  路  3Comments  路  Source: casesandberg/react-color

I upgraded from v2.17.3 to v2.18.1, now I can't pick a color anymore. Tested on the latest Chrome and Firefox.

p.s. I used SketchPicker.

Most helpful comment

I had the exact same issue with the out-of-box SketchPicker component.
I was able to get it to work by specifying the component functions:

import { SketchPicker } from 'react-color';

class Component extends React.Component {
  state = {
    background: '#fff',
  };

  handleChange = (color) => {
    this.setState({ background: color.hex });
  };

  handleChangeComplete = (color) => {
    this.setState({ background: color.hex });
  }

  render() {
    return <SketchPicker
      color={ this.state.background }
      onChange={ this.handleChange }
      onChangeComplete={ this.handleChangeComplete }
    />;
  }
}

and then calling it with

<Component/>

Examples/API helps!

All 3 comments

I had the exact same issue with the out-of-box SketchPicker component.
I was able to get it to work by specifying the component functions:

import { SketchPicker } from 'react-color';

class Component extends React.Component {
  state = {
    background: '#fff',
  };

  handleChange = (color) => {
    this.setState({ background: color.hex });
  };

  handleChangeComplete = (color) => {
    this.setState({ background: color.hex });
  }

  render() {
    return <SketchPicker
      color={ this.state.background }
      onChange={ this.handleChange }
      onChangeComplete={ this.handleChangeComplete }
    />;
  }
}

and then calling it with

<Component/>

Examples/API helps!

Thank you. It seems that it only works by manually changing the state. But I think this is also a small issue, because people may only want to use the picker and simply listen to the onChange event.

I updated the code to also include the handleChangeComplete call and handler. You can customize your listen calls under either of those. onChange calls its handler continuously on drag. onChangeComplete calls its handler once a drag is done. You can test it with a console.log().

Was this page helpful?
0 / 5 - 0 ratings

Related issues

daKmoR picture daKmoR  路  5Comments

dsumer picture dsumer  路  4Comments

Prinzhorn picture Prinzhorn  路  6Comments

jimmylinh picture jimmylinh  路  4Comments

Glutnix picture Glutnix  路  4Comments