Skip to content

Commit d2a040c

Browse files
committed
Code for 6-loading
1 parent 38e76ee commit d2a040c

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

app/components/Loading.js

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,29 @@ const styles = {
1212
}
1313
}
1414

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)
1917

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+
})
2425
}, 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-
}
3726

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+
)
4135
}
4236

43-
Loading.defaultProps = {
44-
text: 'Loading',
45-
speed: 300
37+
Loading.propTypes = {
38+
text: PropTypes.string,
39+
speed: PropTypes.number,
4640
}

0 commit comments

Comments
 (0)