Skip to content

Commit 5214914

Browse files
CopilotTechQuery
andauthored
refactor: replace componentDidUpdate with @reaction from mobx-react-helper
Agent-Logs-Url: https://github.com/Open-Source-Bazaar/Open-Source-Bazaar.github.io/sessions/4c1cea16-bd51-429b-9ddf-9fea961418e1 Co-authored-by: TechQuery <19969570+TechQuery@users.noreply.github.com>
1 parent 867f0ca commit 5214914

1 file changed

Lines changed: 14 additions & 22 deletions

File tree

components/Activity/Hackathon/Countdown.tsx

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { computed, observable } from 'mobx';
22
import { observer } from 'mobx-react';
3-
import { Component } from 'react';
3+
import { ObservedComponent, reaction } from 'mobx-react-helper';
44

55
import styles from './Hero.module.less';
66

@@ -10,14 +10,14 @@ export interface CountdownProps {
1010
}
1111

1212
@observer
13-
export class Countdown extends Component<CountdownProps> {
13+
export class Countdown extends ObservedComponent<CountdownProps> {
1414
@observable
1515
accessor rest: number | null = null;
1616

1717
private timer?: number;
1818

1919
private get target() {
20-
const { countdownTo } = this.props;
20+
const { countdownTo } = this.observedProps;
2121
const value = countdownTo ? new Date(countdownTo).getTime() : NaN;
2222

2323
return Number.isFinite(value) ? value : NaN;
@@ -42,35 +42,27 @@ export class Countdown extends Component<CountdownProps> {
4242
this.rest = Math.max(0, this.target - Date.now());
4343
};
4444

45+
@reaction((_this: Countdown) => _this.observedProps.countdownTo)
4546
componentDidMount() {
46-
if (Number.isFinite(this.target)) {
47-
this.tick();
48-
this.timer = window.setInterval(this.tick, 1000);
49-
}
50-
}
51-
52-
componentDidUpdate(prevProps: CountdownProps) {
53-
if (prevProps.countdownTo !== this.props.countdownTo) {
54-
if (this.timer) {
55-
window.clearInterval(this.timer);
56-
this.timer = undefined;
57-
}
47+
super.componentDidMount();
5848

59-
this.rest = null;
60-
61-
if (Number.isFinite(this.target)) {
62-
this.tick();
63-
this.timer = window.setInterval(this.tick, 1000);
64-
}
49+
if (this.timer) {
50+
window.clearInterval(this.timer);
51+
this.timer = undefined;
6552
}
53+
54+
this.tick();
55+
this.timer = window.setInterval(this.tick, 1000);
6656
}
6757

6858
componentWillUnmount() {
59+
super.componentWillUnmount();
60+
6961
if (this.timer) window.clearInterval(this.timer);
7062
}
7163

7264
render() {
73-
const { unitLabels } = this.props;
65+
const { unitLabels } = this.observedProps;
7466
const { sections } = this;
7567

7668
return (

0 commit comments

Comments
 (0)