|
1 |
| -import React from 'react'; |
| 1 | +import React, { useState, useEffect } from 'react'; |
2 | 2 | import ReactDOM from 'react-dom';
|
3 | 3 | import SeasonDisplay from './SeasonDisplay';
|
4 | 4 | import Spinner from './Spinner';
|
5 | 5 |
|
6 |
| -class App extends React.Component { |
7 |
| - state = { lat: null, errorMessage: '' }; |
| 6 | +const App = () => { |
| 7 | + const [lat, setLat] = useState(null); |
| 8 | + const [errorMessage, setErrorMessage] = useState(''); |
8 | 9 |
|
9 |
| - componentDidMount() { |
| 10 | + useEffect(() => { |
10 | 11 | window.navigator.geolocation.getCurrentPosition(
|
11 |
| - position => this.setState({ lat: position.coords.latitude }), |
12 |
| - err => this.setState({ errorMessage: err.message }) |
| 12 | + position => setLat(position.coords.latitude), |
| 13 | + err => setErrorMessage(err.message) |
13 | 14 | );
|
14 |
| - } |
15 |
| - |
16 |
| - renderContent() { |
17 |
| - if (this.state.errorMessage && !this.state.lat) { |
18 |
| - return <div>Error: {this.state.errorMessage}</div>; |
19 |
| - } |
| 15 | + }, []); |
20 | 16 |
|
21 |
| - if (!this.state.errorMessage && this.state.lat) { |
22 |
| - return <SeasonDisplay lat={this.state.lat} />; |
23 |
| - } |
24 |
| - |
25 |
| - return <Spinner message="Please accept location request" />; |
| 17 | + let content; |
| 18 | + if (errorMessage) { |
| 19 | + content = <div>Error: {errorMessage}</div>; |
| 20 | + } else if (lat) { |
| 21 | + content = <SeasonDisplay lat={lat} />; |
| 22 | + } else { |
| 23 | + content = <Spinner message="Please accept location request" />; |
26 | 24 | }
|
27 | 25 |
|
28 |
| - render() { |
29 |
| - return <div className="border red">{this.renderContent()}</div>; |
30 |
| - } |
31 |
| -} |
| 26 | + return <div className="border red">{content}</div>; |
| 27 | +}; |
32 | 28 |
|
33 | 29 | ReactDOM.render(<App />, document.querySelector('#root'));
|
0 commit comments