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


function Chat(props) {
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  const [inner, setInner] = useState([''])
  const [chat, setChat] = useState([inner])   //object로 key는 보낸사람 value는 메세지

  function handleChange(e) {
    e.preventDefault()
    setInner(e.target.value)
    console.log(e.target.value)
  }

  function sendMsgCH(e) {
    e.preventDefault()
    props.setSingleChat(inner)
    props.sendMsg(e)
    setInner('')
  }

  useEffect(() => {
    setChat([...chat, props.singleChat])
  }, [props.singleChat])

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


  );
}

export default Chat;