@@ -12,35 +12,29 @@ const styles = {
12
12
}
13
13
}
14
14
15
- export default class Loading extends React . Component {
16
- state = { content : this . props . text }
17
- componentDidMount ( ) {
18
- const { speed, text } = this . props
15
+ export default function Loading ( { text = 'Loading' , speed = 300 } ) {
16
+ const [ content , setContent ] = React . useState ( text )
19
17
20
- this . interval = window . setInterval ( ( ) => {
21
- this . state . content === text + '...'
22
- ? this . setState ( { content : text } )
23
- : this . setState ( ( { content } ) => ( { content : content + '.' } ) )
18
+ React . useEffect ( ( ) => {
19
+ const id = window . setInterval ( ( ) => {
20
+ setContent ( ( content ) => {
21
+ return content === `${ text } ...`
22
+ ? text
23
+ : `${ content } .`
24
+ } )
24
25
} , speed )
25
- }
26
- componentWillUnmount ( ) {
27
- window . clearInterval ( this . interval )
28
- }
29
- render ( ) {
30
- return (
31
- < p style = { styles . content } >
32
- { this . state . content }
33
- </ p >
34
- )
35
- }
36
- }
37
26
38
- Loading . propTypes = {
39
- text : PropTypes . string . isRequired ,
40
- speed : PropTypes . number . isRequired ,
27
+ return ( ) => window . clearInterval ( id )
28
+ } , [ text , speed ] )
29
+
30
+ return (
31
+ < p style = { styles . content } >
32
+ { content }
33
+ </ p >
34
+ )
41
35
}
42
36
43
- Loading . defaultProps = {
44
- text : 'Loading' ,
45
- speed : 300
37
+ Loading . propTypes = {
38
+ text : PropTypes . string ,
39
+ speed : PropTypes . number ,
46
40
}
0 commit comments