ReactJS: add loading state

- use 8081 port for talking to SpringBoot (as 8080 occupied by Corda)
This commit is contained in:
djmil 2023-08-19 19:59:55 +02:00
parent 55d086bbe2
commit db3cf0ee29
2 changed files with 14 additions and 13 deletions

View File

@ -17,7 +17,7 @@
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:8080",
"proxy": "http://localhost:8081",
"eslintConfig": {
"extends": [
"react-app",

View File

@ -3,24 +3,25 @@ import React, { useState, useEffect } from 'react';
function App() {
const [joke, setJoke] = useState([]);
const [joke, setJoke] = useState(null);
useEffect(() => {
fetch('/api/badjokes')
.then((response) => response.json())
.then((data) => {
console.log(data.joke);
setJoke(data.joke);
})
.catch((err) => {
console.log(err.message);
});
fetch('/api/badjokes')
.then((response) => response.json())
.then((data) => {
console.log(data.joke);
setJoke(data.joke);
})
.catch((err) => {
console.log(err.message);
});
}, []);
return (
<div className="App">
<header className="App-header">
<p>
Here is your joke: {joke}
Here is your joke: {joke ? <span>{joke}</span> : <span>Loading...</span>}
</p>
</header>
</div>