ChatJS
1import React, { useState, useEffect } from "react"; 2import Input from "../../components/UI/ChatUI/Input/Input"; 3import MessagesChat from "../../../src/components/UI/ChatUI/Messages/Messages"; 4import "./Chat.css"; 5import Sidebar from "../../components/UI/Sidebar/Sidebar"; 6import Midlebar from "../../components/UI/ChatUI/Midlebar/Midlebar"; 7import { Col, Row } from "react-bootstrap"; 8// import axios from "../../axios"; 9import ScroolToBottom from "react-scroll-to-bottom"; 10import { css } from "glamor"; 11 12const Chat = () => { 13 const [Message, setMessage] = useState(""); 14 const [Name, setName] = useState(""); 15 const [messages, setMessages] = useState([]); 16 17 const submitHandler = event => { 18 event.preventDefault(); 19 addIngredientHandler({ name: Name, text: Message }); 20 }; 21 22 useEffect(() => { 23 const name = localStorage.getItem("name"); 24 setName(name); 25 }, []); 26 27 useEffect(() => { 28 fetch("https://react-hooks-update-676a6.firebaseio.com/ingredients.json") 29 .then(response => response.json()) 30 .then(responseData => { 31 console.log(responseData); 32 const loadedIngredients = []; 33 for (const key in responseData) { 34 loadedIngredients.push({ 35 id: key, 36 name: responseData[key].name, 37 text: responseData[key].text 38 }); 39 } 40 setMessages(loadedIngredients); 41 }); 42 }, []); 43 44 const ROOT_CSS = css({ 45 height: 600, 46 width: 650 47 }); 48 49 const addIngredientHandler = text => { 50 fetch("https://react-hooks-update-676a6.firebaseio.com/ingredients.json", { 51 method: "POST", 52 body: JSON.stringify(text), 53 headers: { "Content-Type": "application/json" } 54 }) 55 .then(response => { 56 return response.json(); 57 }) 58 .then(responseData => { 59 setMessages(prevIngredients => [ 60 ...prevIngredients, 61 { id: responseData.name, ...text } 62 ]); 63 }); 64 }; 65 66 return ( 67 <div> 68 <Row style={{ width: "100%" }}> 69 <Col md={3}> 70 <div style={{ paddingLeft: "30px" }}> 71 <Sidebar /> 72 </div> 73 </Col> 74 <Col md={3}> 75 <div> 76 <Midlebar /> 77 </div> 78 </Col> 79 <Col md={6}> 80 <div style={{ textAlign: "right" }}> 81 <div className="chatContainer"> 82 <ScroolToBottom className={ROOT_CSS}> 83 <MessagesChat messages={messages} /> 84 </ScroolToBottom> 85 <form onSubmit={submitHandler}> 86 <Input 87 Message={Message} 88 setMessage={setMessage} 89 submitHandler={submitHandler} 90 /> 91 </form> 92 </div> 93 </div> 94 </Col> 95 </Row> 96 </div> 97 ); 98}; 99export default Chat; 100
MessagesJS
1import React from "react"; 2import "./Messages.css"; 3const Messages = ({ messages }) => ( 4 <div> 5 {messages.map(i => ( 6 <div key={i}> 7 <div className="messageContainer justifyEnd"> 8 <div className="messageBox backgroundBlue"> 9 <p className="messageText colorWhite">{i.text}</p> 10 </div> 11 <p className="sentText pr-10">{i.name}</p> 12 </div> 13 <div className="messageContainer justifyStart"> 14 <p className="sentText pl-10 ">相手</p> 15 <div className="messageBox backgroundLight"> 16 <p className="messageText colorDark">久美子よ</p> 17 </div> 18 </div> 19 </div> 20 ))} 21 </div> 22); 23export default Messages; 24
現在Firebaseを使ってチャットアプリを作っているのですが、相手の名前をどうしても取得することができません。
自分の名前に関しましては、signup時にLocalStorageに保管してそれをとってきて取得できるのですが本来チャットアプリを開発する際はどのようにして相手の名前を取得することができるのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/07 09:20
2020/04/07 18:22