From 8cc3d4c0abf929618a152f78657682c073df7838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?pwliuab=E2=80=9C?= Date: Sat, 2 Apr 2022 14:07:47 +0800 Subject: [PATCH 1/3] fix until does not update when state.property is changed - stays one update behind. --- index.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b546b82..e7e43cf 100644 --- a/index.js +++ b/index.js @@ -43,6 +43,7 @@ class CountDown extends React.Component { until: Math.max(this.props.until, 0), lastUntil: null, wentBackgroundAt: null, + isStartedCountDown: false, }; constructor(props) { @@ -60,6 +61,16 @@ class CountDown extends React.Component { } componentDidUpdate(prevProps, prevState) { + // the original code this.pros.until !== prevProps.until ... always false before timer is started + // here we add inital check for timer, ensure every change in the timer will update the rendering. + if (!isStartedCountDown) { + // this code will not be called when the timer started. + this.setState({ + lastUntil: prevState.until, + until: Math.max(prevProps.until, 0) + }); + } + if (this.props.until !== prevProps.until || this.props.id !== prevProps.id) { this.setState({ lastUntil: prevState.until, @@ -123,6 +134,9 @@ class CountDown extends React.Component { if (this.props.onChange) { this.props.onChange(this.state.until); } + + this.state.isStartedCountDown = true; + this.setState({ lastUntil: this.state.until, until: Math.max(0, this.state.until - 1) @@ -134,7 +148,7 @@ class CountDown extends React.Component { const {digitStyle, digitTxtStyle, size} = this.props; return ( From 0b05c159bdcc3a3354838f697030fd0049e98a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?pwliuab=E2=80=9C?= Date: Sat, 2 Apr 2022 14:10:17 +0800 Subject: [PATCH 2/3] until does not update when state.property is changed - stays one update behind. #102 --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index e7e43cf..56ccc09 100644 --- a/index.js +++ b/index.js @@ -63,7 +63,7 @@ class CountDown extends React.Component { componentDidUpdate(prevProps, prevState) { // the original code this.pros.until !== prevProps.until ... always false before timer is started // here we add inital check for timer, ensure every change in the timer will update the rendering. - if (!isStartedCountDown) { + if (!this.state.isStartedCountDown) { // this code will not be called when the timer started. this.setState({ lastUntil: prevState.until, From e051becbee5a2cad70409ac6e2603b9a983c99f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?pwliuab=E2=80=9C?= Date: Sat, 2 Apr 2022 14:25:58 +0800 Subject: [PATCH 3/3] until does not update when state property is changed --- index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 56ccc09..66987b1 100644 --- a/index.js +++ b/index.js @@ -63,15 +63,13 @@ class CountDown extends React.Component { componentDidUpdate(prevProps, prevState) { // the original code this.pros.until !== prevProps.until ... always false before timer is started // here we add inital check for timer, ensure every change in the timer will update the rendering. - if (!this.state.isStartedCountDown) { + if (!this.state.isStartedCountDown && (this.props.until !== prevState.until || this.props.id !== prevState.id)) { // this code will not be called when the timer started. this.setState({ lastUntil: prevState.until, until: Math.max(prevProps.until, 0) }); - } - - if (this.props.until !== prevProps.until || this.props.id !== prevProps.id) { + } else if (this.props.until !== prevProps.until || this.props.id !== prevProps.id) { this.setState({ lastUntil: prevState.until, until: Math.max(prevProps.until, 0)