From 298d785c612655667998471f832bb6527dab0df0 Mon Sep 17 00:00:00 2001 From: Hussain Marvi Date: Mon, 30 Jul 2018 11:39:33 +0500 Subject: [PATCH 1/2] - added state to update checkbox --- index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/index.js b/index.js index 75e5621..d3b8bda 100755 --- a/index.js +++ b/index.js @@ -59,6 +59,13 @@ export default class CheckBox extends Component { } return null; } + + componentWillReceiveProps(nextProps) { + if (this.props.isChecked !== nextProps.isChecked) { + this.setState({ isChecked: nextProps.isChecked }); + } + } + onClick() { const checkboxState = !this.state.isChecked; this.setState({ From 5f4c154a959d812d2206bc6210e76b0d43fc8f32 Mon Sep 17 00:00:00 2001 From: Hussain Marvi Date: Mon, 30 Jul 2018 11:55:51 +0500 Subject: [PATCH 2/2] - added setState to getDerivedStateFromProps to save isChecked state --- index.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index d3b8bda..198aae5 100755 --- a/index.js +++ b/index.js @@ -51,8 +51,9 @@ export default class CheckBox extends Component { rightTextStyle: {} } - static getDerivedStateFromProps(nextProps, prevState) { + getDerivedStateFromProps(nextProps, prevState) { if (prevState.isChecked !== nextProps.isChecked) { + this.setState({ isChecked: nextProps.isChecked }); return { isChecked: nextProps.isChecked }; @@ -60,12 +61,6 @@ export default class CheckBox extends Component { return null; } - componentWillReceiveProps(nextProps) { - if (this.props.isChecked !== nextProps.isChecked) { - this.setState({ isChecked: nextProps.isChecked }); - } - } - onClick() { const checkboxState = !this.state.isChecked; this.setState({