git: https://github.com/leeyulgok/4-Day3
- Actions : 애플리케이션에서 무엇이 일어나는지 설명하는 객체.
모든 action은 type 속성이 필수.- Reducers : 액션에 따라 상태가 어떻게 바뀌는지를 명시하는 함수. 이전 상태와 액션을 인수로 받아 새로운 상태를 반환.
- Store : 애플리케이션의 상태를 보유. 애플리케이션의 상태를 조회하거나, 상태를 업데이트하는 액션을 dispatch 할 수 있음.
+) dispatch : Store에 액션을 보내는 함수. 이 액션은 Reducer로 전달되고, Reducer는 액션 타입에 따라 새로운 상태를 생성.
import React, { useReducer } from "react";
const initialState = 0;
const reducer = (state, action) => {
switch (action.type) {
case "INCREMENT":
return state + 1;
case "DECREMENT":
return state - 1;
case "RESET":
return initialState;
default:
return state;
}
};
const CountReducer = () => {
const [count, dispatch] = useReducer(reducer, initialState);
return (
<div>
<h1>숫자세기</h1>
<p>값 : {count}</p>
<button onClick={() => dispatch({ type: "INCREMENT" })}>증가</button>
<button onClick={() => dispatch({ type: "DECREMENT" })}>감소</button>
<button onClick={() => dispatch({ type: "RESET" })}>리셋</button>
</div>
);
};
export default CountReducer;
npm i socket.io-client
npm i express socket.io
const express = require("express");
const http = require("http");
const socketIO = require("socket.io");
const cors = require("cors");
const app = express();
app.use(cors());
const server = http.createServer(app);
const io = socketIO(server, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"],
},
});
io.on("connection", (socket) => {
console.log("새로운 클라이언트가 연결되었습니다.");
socket.on("message", (message) => {
console.log("새로운 메시지:", message);
io.emit("message", message);
});
// 클라이언트 연결 해제 처리
socket.on("disconnect", () => {
console.log("클라이언트가 연결을 해제하였습니다.");
});
});
server.listen(3001, () => {
console.log("서버가 3001 포트에서 실행 중입니다.");
});
import React, { useState, useEffect } from "react";
import { io } from "socket.io-client";
const socket = io("http://localhost:3001");
const Chat = () => {
const [username, setUsername] = useState("");
const [message, setMessage] = useState([]);
const [inputValue, setInputValue] = useState("");
useEffect(() => {
socket.on("message", sumitHandler);
return () => {
socket.off("message", sumitHandler);
};
});
const sumitHandler = message => {
setMessage((prevMessages) => [...prevMessages, message]);
}
const userHandler = (event) => {
setUsername(event.target.value);
};
const messageHandler = () => {
if (inputValue.trim() !== "") {
const currentTime = new Date().toLocaleDateString();
socket.emit("message", {
username,
content: inputValue,
time: currentTime,
});
console.log();
setInputValue("");
}
};
return (
<div>
<h1>실시간 채팅</h1>
<input
type="text"
value={username}
onChange={userHandler}
placeholder="사용자명"
/>
<h1>채팅방</h1>
<div>
{message.map((m, ind) => (
<p key={ind}>
{m.username} : {m.content} - {m.time}
</p>
))}
</div>
<input
type="text"
value={inputValue}
onChange={e => setInputValue(e.target.value)}
/>
<button onClick={messageHandler}>전송</button>
</div>
);
};
export default Chat;
- 매일 글쓰기.
- 복습하기
- 스스로 다른 거 해보기
- 프로젝트 준비하기.
- 프로젝트 준비를 위한 리액트 예습하기
본 후기는 유데미-스나이퍼팩토리 10주 완성 프로젝트캠프 학습 일지 후기로 작성 되었습니다.
#프로젝트캠프 #프로젝트캠프후기 #유데미 #스나이퍼팩토리 #웅진씽크빅 #인사이드아웃 #IT개발캠프 #개발자부트캠프 #리액트 #react #부트캠프 #리액트캠프