Chat.js 1.37 KB
Newer Older
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
1
import React, { useState, useEffect } from 'react';
2
import { Form, Button, Row } from 'react-bootstrap';
우지원's avatar
ul    
우지원 committed
3
4
5


function Chat(props) {
Choi Ga Young's avatar
aa    
Choi Ga Young committed
6
  let defaultname = sessionStorage.getItem('name');
JeongYeonwoo's avatar
JeongYeonwoo committed
7

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
8
9
10
  const [inner, setInner] = useState([''])
  const [chat, setChat] = useState([inner])   //object로 key는 보낸사람 value는 메세지

11
12
  function handleChange(e) {
    e.preventDefault()
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
13
    setInner(e.target.value)
14
15
16
17
    console.log(e.target.value)
  }

  function sendMsgCH(e) {
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
18
19
    e.preventDefault()
    props.setSingleChat(inner)
20
    props.sendMsg(e)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
21
    setInner('')
22
23
  }

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
24
25
26
  useEffect(() => {
    setChat([...chat, props.singleChat])
  }, [props.singleChat])
Choi Ga Young's avatar
aa    
Choi Ga Young committed
27

우지원's avatar
ul    
우지원 committed
28
  return (
JeongYeonwoo's avatar
JeongYeonwoo committed
29

우지원's avatar
ul    
우지원 committed
30
    <div className="chat" id="chat" style={{ border: "2px solid", height: "300%", margin: "1%", borderColor: "#BDBDBD" }}>
31
      <h2>현재 {props.roomName} 입니다.</h2>
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
32
      {      chat.map((value, index) => (
33
        <Row key={index} className='ml-3'>
Choi Ga Young's avatar
aa    
Choi Ga Young committed
34
          {props.roomName}에서 {defaultname}님이 보낸 메세지 : {value}
35
36
        </Row>
      ))}
우지원's avatar
ul    
우지원 committed
37
      <Button variant="light" onClick={props.handleChatc} >{`<`}</Button>
38
      <Form onSubmit={sendMsgCH}>
우지원's avatar
ul    
우지원 committed
39
        <Form.Group>
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
40
          <Form.Control name='chat' type="text" value={inner} onChange={handleChange} />
우지원's avatar
ul    
우지원 committed
41
42
43
44
        </Form.Group>
        <Button variant="primary" type="submit">
          전송
        </Button>
45
      </Form>
우지원's avatar
ul    
우지원 committed
46
47
48
49
50
51
52
    </div>


  );
}

export default Chat;