react-native-check-box icon indicating copy to clipboard operation
react-native-check-box copied to clipboard

Wrong checked/unchecked logic

Open MartinMobilize opened this issue 8 years ago • 4 comments

The component does not consider the 'isChecked' property.

The component refers all the time to this.state to check if the button is checked or not, and at the onClick event it just change the this.state.

This is not good. We need to rely on the this.props to let everyone to control the behavior.

1- Remove this.setState from onClick(). 2- on _renderImage() check this.props instead this.state 3- on genCheckedImage() check this.props instead this.state.

This is how these functions should look like:

_renderImage() {
    if (this.props.isChecked) {
        return this.props.checkedImage ? this.props.checkedImage : this.genCheckedImage();
    } else {
        return this.props.unCheckedImage ? this.props.unCheckedImage : this.genCheckedImage();
    }
}

genCheckedImage() {
    var source = this.props.isChecked ? require('./img/ic_check_box.png') : require('./img/ic_check_box_outline_blank.png');

    return (
        <Image source={source}/>
    )
}

onClick() {
    this.props.onClick();
}

MartinMobilize avatar Feb 23 '17 09:02 MartinMobilize

@crazycodeboy Why this is not fixed yet?? More then a year now

wzup avatar Jun 20 '18 11:06 wzup

I have the same problem here. I use RN 0.52.2

DavitVosk avatar Jun 21 '18 10:06 DavitVosk

Guys you can use https://github.com/DavitVosk/react-native-check-box.git fork

DavitVosk avatar Jun 21 '18 11:06 DavitVosk

Someone turn this into a PR?

That fork changes are only this:

  componentWillReceiveProps(nextProps) {
    if (this.props.isChecked !== nextProps.isChecked) {
      this.setState({ isChecked: nextProps.isChecked })
    }
  }

Doesn't seem to be related to the suggestions above.

This is working for me now:

            <CheckBox
              isChecked={this.state.acceptedTermsAndConditions}
              onClick={() => this.setState({ acceptedTermsAndConditions: !this.state.acceptedTermsAndConditions })}

dblock avatar Jul 28 '18 19:07 dblock