Merging dev into main #49
68
src/App.tsx
68
src/App.tsx
@@ -1,35 +1,55 @@
|
|||||||
import { useState } from 'react'
|
import { useState, useEffect } from 'react'
|
||||||
import reactLogo from './assets/react.svg'
|
|
||||||
import viteLogo from '/vite.svg'
|
|
||||||
import './App.css'
|
import './App.css'
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [count, setCount] = useState(0)
|
const [message, setMessage] = useState('');
|
||||||
|
const [sseMessage, setSseMessage] = useState('');
|
||||||
|
|
||||||
|
const sendMessage = async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch("http://localhost:8000/message", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ message }),
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
console.log(data);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error sending message: ", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const eventSource = new EventSource("http://localhost:8000/sse");
|
||||||
|
|
||||||
|
eventSource.onmessage = (event) => {
|
||||||
|
setSseMessage(event.data);
|
||||||
|
};
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
eventSource.close();
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<div className="App">
|
||||||
<div>
|
<div>
|
||||||
<a href="https://vite.dev" target="_blank">
|
<input
|
||||||
<img src={viteLogo} className="logo" alt="Vite logo" />
|
type="text"
|
||||||
</a>
|
value={message}
|
||||||
<a href="https://react.dev" target="_blank">
|
onChange={(e) => setMessage(e.target.value)}
|
||||||
<img src={reactLogo} className="logo react" alt="React logo" />
|
placeholder="Enter a message"
|
||||||
</a>
|
/>
|
||||||
|
<button onClick={sendMessage}>Send Message to Backend</button>
|
||||||
</div>
|
</div>
|
||||||
<h1>Vite + React</h1>
|
<div>
|
||||||
<div className="card">
|
<h2>Message from Server (SSE):</h2>
|
||||||
<button onClick={() => setCount((count) => count + 1)}>
|
<p>{sseMessage}</p>
|
||||||
count is {count}
|
|
||||||
</button>
|
|
||||||
<p>
|
|
||||||
Edit <code>src/App.tsx</code> and save to test HMR
|
|
||||||
</p>
|
|
||||||
</div>
|
</div>
|
||||||
<p className="read-the-docs">
|
</div>
|
||||||
Click on the Vite and React logos to learn more
|
);
|
||||||
</p>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default App
|
export default App
|
||||||
|
|||||||
Reference in New Issue
Block a user