Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix until does not update when state.property is changed , stays one state behind. #109

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class CountDown extends React.Component {
until: Math.max(this.props.until, 0),
lastUntil: null,
wentBackgroundAt: null,
isStartedCountDown: false,
};

constructor(props) {
Expand All @@ -60,7 +61,15 @@ class CountDown extends React.Component {
}

componentDidUpdate(prevProps, prevState) {
if (this.props.until !== prevProps.until || this.props.id !== prevProps.id) {
// 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 && (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)
});
} else if (this.props.until !== prevProps.until || this.props.id !== prevProps.id) {
this.setState({
lastUntil: prevState.until,
until: Math.max(prevProps.until, 0)
Expand Down Expand Up @@ -123,6 +132,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)
Expand All @@ -134,7 +146,7 @@ class CountDown extends React.Component {
const {digitStyle, digitTxtStyle, size} = this.props;
return (
<View style={[
styles.digitCont,
styles.digitCont,
{width: size * 2.3, height: size * 2.6},
digitStyle,
]}>
Expand Down