71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
import React, { useEffect, useState } from 'react';
|
|
import logo from './logo.svg';
|
|
import './App.css';
|
|
import { Client, WeatherForecast } from './ApiCient';
|
|
|
|
function App() {
|
|
|
|
const [forecast, setForecast] = useState<WeatherForecast[] | undefined>([]);
|
|
|
|
|
|
useEffect(() => {
|
|
const client = new Client();
|
|
|
|
const fetchForecast = async () => {
|
|
const forecast = await client.getWeatherForecast();
|
|
setForecast(forecast);
|
|
};
|
|
|
|
fetchForecast();
|
|
const intervalId = setInterval(fetchForecast, 1000);
|
|
|
|
return () => clearInterval(intervalId);
|
|
}, []);
|
|
|
|
|
|
return (
|
|
<div className="App">
|
|
{/* <header className="App-header">
|
|
<img src={logo} className="App-logo" alt="logo" />
|
|
<p>
|
|
Edit <code>src/App.tsx</code> and save to reload.
|
|
</p>
|
|
<a
|
|
className="App-link"
|
|
href="https://reactjs.org"
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
>
|
|
Learn React
|
|
</a>
|
|
</header> */}
|
|
<div className='text-3xl'>
|
|
Testing
|
|
</div>
|
|
|
|
<button className='button' onClick={async () => {
|
|
const client = new Client();
|
|
const forecast = await client.getWeatherForecast();
|
|
setForecast(forecast);
|
|
}} > Call API</button>
|
|
|
|
|
|
{forecast?.map((item: WeatherForecast) => {
|
|
return (
|
|
<div key={item.date?.toString()}>
|
|
<div> {item.date?.toDateString()} </div>
|
|
<div> {item.temperatureC} </div>
|
|
<div> {item.temperatureF} </div>
|
|
<div> {item.summary} </div>
|
|
</div>
|
|
)
|
|
})
|
|
}
|
|
|
|
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default App;
|