Skip to content

Commit a2bb227

Browse files
spyipcorinagum
authored andcommitted
Use React Hooks for Timer (#2546)
* Update Timer.js to use React Hooks * Add entry * Update CHANGELOG
1 parent c78f82f commit a2bb227

2 files changed

Lines changed: 12 additions & 33 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
4949
- PR [#2551](https://github.com/microsoft/BotFramework-WebChat/pull/2551): `useLastTypingAt`, `useSendTypingIndicator`, `useTypingIndicator`
5050
- PR [#2552](https://github.com/microsoft/BotFramework-WebChat/pull/2552): `useFocusSendBox`, `useScrollToEnd`, `useSendBoxValue`, `useSubmitSendBox`, `useTextBoxSubmit`, `useTextBoxValue`
5151
- Fixes [#2597](https://github.com/microsoft/BotFramework-WebChat/issues/2597). Modify `watch` script to `start` and add `tableflip` script for throwing `node_modules`, by [@corinagum](https://github.com/corinagum) in PR [#2598](https://github.com/microsoft/BotFramework-WebChat/pull/2598)
52-
53-
### Fixed
52+
- `component`: Fixes [#2331](https://github.com/microsoft/BotFramework-WebChat/issues/2331). Updated timer to use React Hooks, by [@spyip](https://github.com/spyip) in PR [#2546](https://github.com/microsoft/BotFramework-WebChat/pull/2546)
5453

5554
### Changed
5655

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,21 @@
11
/* eslint react/no-unsafe: off */
22

3+
import { useEffect } from 'react';
34
import PropTypes from 'prop-types';
4-
import React from 'react';
5-
6-
export default class Timer extends React.Component {
7-
componentDidMount() {
8-
const { at } = this.props;
9-
10-
this.schedule(at);
11-
}
12-
13-
UNSAFE_componentWillReceiveProps({ at: nextAt }) {
14-
const { at } = this.props;
15-
16-
if (at !== nextAt) {
17-
this.schedule(nextAt);
18-
}
19-
}
20-
21-
componentWillUnmount() {
22-
clearTimeout(this.timeout);
23-
}
24-
25-
schedule(at) {
26-
clearTimeout(this.timeout);
275

6+
const Timer = ({ at, onInterval }) => {
7+
useEffect(() => {
288
if (!isNaN(at)) {
29-
this.timeout = setTimeout(() => {
30-
const { onInterval } = this.props;
31-
9+
const timeout = setTimeout(() => {
3210
onInterval && onInterval();
3311
}, Math.max(0, at - Date.now()));
12+
13+
return () => clearTimeout(timeout);
3414
}
35-
}
15+
}, [at, onInterval]);
3616

37-
render() {
38-
return false;
39-
}
40-
}
17+
return false;
18+
};
4119

4220
Timer.defaultProps = {
4321
onInterval: undefined
@@ -47,3 +25,5 @@ Timer.propTypes = {
4725
at: PropTypes.number.isRequired,
4826
onInterval: PropTypes.func
4927
};
28+
29+
export default Timer;

0 commit comments

Comments
 (0)