From 698d892188168716eb5b6cae9f6e9483dfd663e6 Mon Sep 17 00:00:00 2001 From: joanShim Date: Fri, 10 Nov 2023 17:16:32 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20=EC=B1=84=ED=8C=85=20input=20=EB=A6=AC?= =?UTF-8?q?=EB=A0=8C=EB=8D=94=EB=A7=81=20=EC=9D=B4=EC=8A=88=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Game/GameChat/index.tsx | 28 +++++++++++++++----------- src/components/common/ChatBubble.tsx | 2 +- src/pages/Game/index.tsx | 22 +++++++++----------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/components/Game/GameChat/index.tsx b/src/components/Game/GameChat/index.tsx index 06f631a7..a3945b11 100644 --- a/src/components/Game/GameChat/index.tsx +++ b/src/components/Game/GameChat/index.tsx @@ -6,17 +6,20 @@ import { InputGroup, InputRightElement, } from "@chakra-ui/react"; -import { useEffect, useState } from "react"; +import { useEffect, useRef, useState } from "react"; import { io } from "socket.io-client"; -import useInput from "../../../hooks/useInput"; import ChatBubble from "../../common/ChatBubble"; interface Message { id: string; text: string; } -const GameChat = ({ gameId }) => { - console.log(gameId); + +interface GameChatProps { + gameId: string; +} + +const GameChat: React.FC = ({ gameId }) => { const token = JSON.parse(localStorage.getItem("token") as string); const socket = io(`https://fastcampus-chat.net/chat?chatId=${gameId}`, { @@ -26,13 +29,12 @@ const GameChat = ({ gameId }) => { }, }); - // 메세지 데이터 const [message, setMessage] = useState({ id: "", text: "", }); const [messages, setMessages]: any = useState([]); - const messageValue = useInput(""); + const messageRef = useRef(null); useEffect(() => { socket.on("message-to-client", (messageObject) => { @@ -53,11 +55,14 @@ const GameChat = ({ gameId }) => { setMessages([...messages, message]); } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [message.text]); + }, [message.text, message.id]); const submitMessage = () => { - socket.emit("message-to-server", messageValue.value); - messageValue.clear(); + if (messageRef.current && messageRef.current.value) { + const messageValue = messageRef.current.value; + socket.emit("message-to-server", messageValue); + messageRef.current.value = ""; + } }; const handleKeyPress = (event: React.KeyboardEvent) => { @@ -69,7 +74,7 @@ const GameChat = ({ gameId }) => { return ( - {messages.map((message, index) => ( + {messages.map((message: Message, index: number) => ( ))} @@ -78,8 +83,7 @@ const GameChat = ({ gameId }) => { pr="4.5rem" type="text" placeholder="채팅내용" - value={messageValue.value} - onChange={messageValue.onChange} + ref={messageRef} onKeyPress={handleKeyPress} /> diff --git a/src/components/common/ChatBubble.tsx b/src/components/common/ChatBubble.tsx index fb69f0d1..71c9d326 100644 --- a/src/components/common/ChatBubble.tsx +++ b/src/components/common/ChatBubble.tsx @@ -16,7 +16,7 @@ interface ChatBubbleProps extends BoxProps { text: string; } -const ChatBubble: React.FC = ({ userId, text, ...rest }) => { +const ChatBubble: React.FC = ({ userId, text }) => { return ( diff --git a/src/pages/Game/index.tsx b/src/pages/Game/index.tsx index 32d2f71d..0ba61a66 100644 --- a/src/pages/Game/index.tsx +++ b/src/pages/Game/index.tsx @@ -1,18 +1,12 @@ +import { Button, Card, Center, Grid, GridItem, Text } from "@chakra-ui/react"; import GameChat from "../../components/Game/GameChat"; -import { - Button, - Card, - Center, - Container, - Flex, - Grid, - GridItem, - Spacer, - Text, -} from "@chakra-ui/react"; import useFireFetch from "../../hooks/useFireFetch"; -const ProfileCard = ({ userId }) => { +interface ProfileCardProps { + userId: string; +} + +const ProfileCard: React.FC = ({ userId }) => { return (
@@ -36,6 +30,10 @@ const Game = () => { return

Loading...

; } + if (!gameId) { + return null; + } + return ( <>