Created
May 30, 2025 13:49
-
-
Save sunmeat/6c7cb721d2eb4db650aac2db5f87f947 to your computer and use it in GitHub Desktop.
черновик уведомления вебсокет
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react'; | |
import './App.css'; | |
function App() { | |
const [orders, setOrders] = useState([]); | |
useEffect(() => { | |
const ws = new WebSocket('ws://your-server.com/orders'); | |
ws.onmessage = (event) => { | |
const newOrder = JSON.parse(event.data); | |
setOrders((prev) => [...prev, newOrder]); | |
}; | |
ws.onclose = () => console.log('WebSocket закрыт'); | |
return () => ws.close(); | |
}, []); | |
return ( | |
<div className="container"> | |
<h1>Новые заказы</h1> | |
<ul> | |
{orders.map((order, index) => ( | |
<li key={index}>{order.product} - {order.price}</li> | |
))} | |
</ul> | |
</div> | |
); | |
} | |
export default App; | |
.container { | |
max-width: 800px; | |
margin: 0 auto; | |
padding: 20px; | |
} | |
ul { | |
list-style: none; | |
padding: 0; | |
} | |
li { | |
padding: 10px; | |
border-bottom: 1px solid #ddd; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment