File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- import { io } from "socket.io-client " ;
2- import "./App.css " ;
1+ import { useState } from "react " ;
2+ import useSocket from "./hooks/useSocket " ;
33
4- const App = ( ) => {
5- const socket = io ( "http://localhost:3000" ) ;
4+ export default function App ( ) {
5+ const { messages, sendMessage } = useSocket ( "http://localhost:3000" ) ;
6+ const [ input , setInput ] = useState ( "" ) ;
67
7- socket . on ( "connect" , ( ) => {
8- console . log ( socket . id ) ;
9- } ) ;
10-
11- socket . on ( "message" , ( data ) => console . log ( data ) ) ;
12-
13- return < > </ > ;
14- } ;
15-
16- export default App ;
8+ const handleSend = ( ) => {
9+ if ( ! input . trim ( ) ) return ;
10+ sendMessage ( input . trim ( ) ) ;
11+ setInput ( "" ) ;
12+ } ;
13+
14+ return (
15+ < div >
16+ < ul >
17+ { messages . map ( ( msg , i ) => (
18+ < li key = { i } > { msg . text } </ li >
19+ ) ) }
20+ </ ul >
21+ < input value = { input } onChange = { ( e ) => setInput ( e . target . value ) } />
22+ < button onClick = { handleSend } > Send</ button >
23+ </ div >
24+ ) ;
25+ }
You can’t perform that action at this time.
0 commit comments