|
10 | 10 |
|
11 | 11 | import type {Node} from 'React';
|
12 | 12 | import {ActivityIndicator, StyleSheet, View} from 'react-native';
|
13 |
| -import React, {Component} from 'react'; |
| 13 | +import React, {useState, useCallback, useRef, useEffect} from 'react'; |
14 | 14 |
|
15 |
| -type State = {|animating: boolean|}; |
16 |
| -type Props = $ReadOnly<{||}>; |
17 |
| -type Timer = TimeoutID; |
| 15 | +function ToggleAnimatingActivityIndicator() { |
| 16 | + const timer = useRef(); |
18 | 17 |
|
19 |
| -class ToggleAnimatingActivityIndicator extends Component<Props, State> { |
20 |
| - _timer: Timer; |
| 18 | + const [animating, setAnimating] = useState(true); |
21 | 19 |
|
22 |
| - constructor(props: Props) { |
23 |
| - super(props); |
24 |
| - this.state = { |
25 |
| - animating: true, |
26 |
| - }; |
27 |
| - } |
28 |
| - |
29 |
| - componentDidMount() { |
30 |
| - this.setToggleTimeout(); |
31 |
| - } |
| 20 | + const setToggleTimeout = useCallback(() => { |
| 21 | + timer.current = setTimeout(() => { |
| 22 | + setAnimating(currentState => !currentState); |
| 23 | + setToggleTimeout(); |
| 24 | + }, 2000); |
| 25 | + }, []); |
32 | 26 |
|
33 |
| - componentWillUnmount() { |
34 |
| - clearTimeout(this._timer); |
35 |
| - } |
| 27 | + useEffect(() => { |
| 28 | + setToggleTimeout(); |
36 | 29 |
|
37 |
| - setToggleTimeout() { |
38 |
| - this._timer = setTimeout(() => { |
39 |
| - this.setState({animating: !this.state.animating}); |
40 |
| - this.setToggleTimeout(); |
41 |
| - }, 2000); |
42 |
| - } |
| 30 | + return () => { |
| 31 | + clearTimeout(timer.current); |
| 32 | + }; |
| 33 | + }, [timer, setToggleTimeout]); |
43 | 34 |
|
44 |
| - render(): Node { |
45 |
| - return ( |
46 |
| - <ActivityIndicator |
47 |
| - animating={this.state.animating} |
48 |
| - style={[styles.centering, {height: 80}]} |
49 |
| - size="large" |
50 |
| - /> |
51 |
| - ); |
52 |
| - } |
| 35 | + return ( |
| 36 | + <ActivityIndicator |
| 37 | + animating={animating} |
| 38 | + style={[styles.centering, {height: 80}]} |
| 39 | + size="large" |
| 40 | + /> |
| 41 | + ); |
53 | 42 | }
|
54 | 43 |
|
55 | 44 | const styles = StyleSheet.create({
|
|
0 commit comments