|
1 |
| -import React from 'react' |
2 |
| -import ReactDOM from 'react-dom' |
| 1 | +import React, { useEffect, useState } from 'react' |
3 | 2 |
|
4 |
| -const App: React.FunctionComponent<{}> = () => ( |
5 |
| - <h1>Hello, to the frontend</h1> |
6 |
| -) |
| 3 | +import { createRoot } from 'react-dom/client' |
7 | 4 |
|
8 |
| -ReactDOM.render(<App />, document.getElementById('root')) |
| 5 | +const container = document.getElementById('root'); |
| 6 | +const root = createRoot(container!); |
| 7 | + |
| 8 | +const App: React.FunctionComponent<{}> = () => { |
| 9 | + const [error, setError] = useState(null); |
| 10 | + const [isLoaded, setIsLoaded] = useState(false); |
| 11 | + const [items, setItems] = useState([]); |
| 12 | + |
| 13 | + useEffect(() => { |
| 14 | + fetch("http://localhost:9000/api", { |
| 15 | + headers: { |
| 16 | + 'Content-Type': 'application/json', |
| 17 | + 'Accept': 'application/json' |
| 18 | + } |
| 19 | + }).then( |
| 20 | + res => res.json() |
| 21 | + ).then( |
| 22 | + (result) => { |
| 23 | + console.log(result); |
| 24 | + }, |
| 25 | + (err) => { |
| 26 | + setIsLoaded(true) |
| 27 | + setError(err) |
| 28 | + } |
| 29 | + ) |
| 30 | + }, []) |
| 31 | + |
| 32 | + if (error) { |
| 33 | + return <div>Error: {error.message}</div>; |
| 34 | + } else if (!isLoaded) { |
| 35 | + return <div>Loading...</div>; |
| 36 | + } else { |
| 37 | + return ( |
| 38 | + <div className='some-element'> |
| 39 | + <h1>Hello, to the frontend!</h1> |
| 40 | + <p>{items.map(item => (item.name))}</p> |
| 41 | + </div> |
| 42 | + ) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +root.render(<App />) |
0 commit comments