React

[TIL/React] 2023/08/21

event page ๊ด€๋ จ ๐ŸŸ  ์ •์œก๊ฐ์˜ event page๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•œ๋‹ค. ๋งˆ์šฐ์Šค๋ฅผ 'press'ํ•˜๊ณ (click๊ณผ ๊ตฌ๋ถ„ํ•  ๊ฒƒ) ์šฐ์ธก์—์„œ ์ขŒ์ธก์œผ๋กœ ๋„˜๊ธฐ๋ฉด, ์ง„ํ–‰์ค‘์ธ event๋“ค์ด oveflow๋˜๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. best practice๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐฉ์‹์„ ํƒํ–ˆ

2023๋…„ 8์›” 21์ผ2min read

event page ๊ด€๋ จ ๐ŸŸ 

์ •์œก๊ฐ์˜ event page๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•œ๋‹ค. ๋งˆ์šฐ์Šค๋ฅผ 'press'ํ•˜๊ณ (click๊ณผ ๊ตฌ๋ถ„ํ•  ๊ฒƒ) ์šฐ์ธก์—์„œ ์ขŒ์ธก์œผ๋กœ ๋„˜๊ธฐ๋ฉด, ์ง„ํ–‰์ค‘์ธ event๋“ค์ด oveflow๋˜๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. best practice๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐฉ์‹์„ ํƒํ–ˆ๋‹ค.

best practice ๐Ÿ”ต

code
import React, { useState } from "react";
import styled from "styled-components";

const CardContainer = styled.div`
  display: flex;
  overflow-x: scroll;
  width: 100%;
  height: 200px;
  background-color: #f0f0f0;
  cursor: grab;
`;

const Card = styled.div`
  flex: 0 0 200px;
  height: 100%;
  background-color: #fff;
  border: 1px solid #ccc;
  margin-right: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
`;

const App = () => {
  const [isMousePressed, setIsMousePressed] = useState(false);
  const [containerScrollLeft, setContainerScrollLeft] = useState(0);
  const [startMouseX, setStartMouseX] = useState(0);

  const handleMousePress = (e) => {
    setIsMousePressed(true);
    setStartMouseX(e.pageX - e.currentTarget.offsetLeft);
    setContainerScrollLeft(e.currentTarget.scrollLeft);
  };

  const handleMouseMove = (e) => {
    if (!isMousePressed) return;
    e.preventDefault();
    const currentMouseX = e.pageX - e.currentTarget.offsetLeft;
    const mouseMovement = currentMouseX - startMouseX;
    console.log(mouseMovement);
    console.log("2", containerScrollLeft);
    e.currentTarget.scrollLeft = containerScrollLeft - mouseMovement;
  };

  const handleMouseRelease = () => {
    setIsMousePressed(false);
  };

  return (
    <CardContainer
      // ๋ˆ„๋ฅด๋Š” ์ˆœ๊ฐ„
      onMouseDown={handleMousePress}
      onMouseMove={handleMouseMove}
      onMouseUp={handleMouseRelease}
      onMouseLeave={handleMouseRelease}
    >
      {/* ์นด๋“œ ์ปดํฌ๋„ŒํŠธ๋“ค */}

      <Card>Card 1</Card>
      <Card>Card 2</Card>
      <Card>Card 3</Card>
      <Card>Card 4</Card>
      <Card>Card 5</Card>
      <Card>Card 1</Card>
      <Card>Card 2</Card>
      <Card>Card 3</Card>
      <Card>Card 4</Card>
      <Card>Card 5</Card>
    </CardContainer>
  );
};

export default App;

card container์— onMouseDown, onMouseMove, onMouseUp, onMouseLeave๊ฐ€ ์กด์žฌํ•œ๋‹ค.

onClick์€ 'ํด๋ฆญ'ํ•  ๋•Œ ํŠน์ • event ๊ฐ์ฒด๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์œ„ ์š”์†Œ๋“ค๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค.

onMouseDown๋Š” ๋งˆ์šฐ์Šค๋กœ pressํ•  ๋•Œ, onMouseMove๋Š” ๋งˆ์šฐ์Šค๋กœ ๋”์—์„œ ์›€์ง์ผ ๋•Œ, onMouseUp๋Š” ๋งˆ์šฐ์Šค๋ฅผ ๋—„ ๋•Œ, onMouseLeave๋Š” ํŠน์ • element์—์„œ ๋งˆ์šฐ์Šค๊ฐ€ ๋– ๋‚ฌ์„ ๋•Œ event ๊ฐ์ฒด๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

1. onMouseDown

onMouseDown์—๋Š” handleMousePress ํ•จ์ˆ˜๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค. isMousePressed๋ผ๋Š” ์ƒํƒœ๋ฅผ true๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. ์ด์–ด์„œ startMouseX๋ผ๋Š” ์ƒํƒœ๋ฅผ, 'press'ํ•˜๋Š” ์ˆœ๊ฐ„ page์˜ x์ขŒํ‘œ์—์„œ card container์˜ offsetLeft๋ฅผ ๋บ€ ๊ฐ’์œผ๋กœ updateํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ containerScrollLeft๋ผ๋Š” ์ƒํƒœ๋ฅผ e.currentTarget.scrollLeft๋กœ updateํ•œ๋‹ค

2. onMouseMove

pressํ•œ ์ƒํƒœ๋ผ๋ฉด if๋ฌธ์„ ํ†ต๊ณผํ•œ ๋’ค preventDefault ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋œ๋‹ค. ์ดํ›„ currentMouseX ์ƒ์ˆ˜์— page์˜ x์ขŒํ‘œ์—์„œ offsetLeft๋ฅผ ๋บ€ ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค. ์›€์ง์ธ ์ดํ›„์˜ x์ขŒํ‘œ๋ฅผ ์˜๋ฏธํ•˜๊ธฐ์— startMouseX์˜ x์ขŒํ‘œ์™€๋Š” ๊ตฌ๋ถ„๋œ๋‹ค๋Š” ์ ์„ ์ธ์ง€ํ•ด์•ผ ํ•œ๋‹ค.

mouseMovement ์ƒ์ˆ˜๋Š” currrent page์˜ x์ขŒํ‘œ์—์„œ start page์˜ x์ขŒํ‘œ๋ฅผ ๋บ€ ๊ฐ’์ด ํ• ๋‹น๋œ๋‹ค.

preventDefault, currentMouseX ํ• ๋‹น, mouseMovement ํ• ๋‹น์ด ์ข…๋ฃŒ๋˜๋ฉด, ๋งˆ์ง€๋ง‰์œผ๋กœ e.currentTarget.scrollLeft ๊ฐ’์„ containerScrollLeft์—์„œ mouseMovement๋ฅผ ๋บ€ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

3. onMouseUp, onMouseLeave

isMousePressed๋ฅผ false๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ๋งŒ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

ํšŒ๊ณ  ๐Ÿ”ด

containerScrollLeft ๋ถ€๋ถ„์ด ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š์•˜๋‹ค. ๋‚ด์ผ์€ containerScrollLeft๊นŒ์ง€ ๋ช…ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•ด์„œ ํ”„๋กœ์ ํŠธ์— ์ ์šฉํ•˜๋Š” ๋ฐ ์„ฑ๊ณตํ•˜๊ธธ ๋ฐ”๋ž€๋‹ค. ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๊ณ  ์ž๋ ค๋‹ˆ๊นŒ ๊ต‰์žฅํžˆ ์ฐ์ฐํ•œ๋ฐ, ์ฒ˜์Œ ์‹œ๋„ํ•ด ๋ณด๋Š” ๊ฑฐ๋‹ˆ๊นŒ ์ฒซ ์ˆ ์— ๋ฐฐ๋ถ€๋ฅด๋ ค ์š•์‹ฌ๋‚ด์ง€ ๋ง์ž. ๋ฐ”๋ณด ๊ฐ์ž ์ฃผ์ œ์— ํ•œ ํ์— ์ดํ•ด๋˜๊ธธ ๋ฐ”๋ผ๋Š” ๊ฑด '์˜ค๋ฒ„'๋‹ค.

More to read

REST API

ํ”„๋ก ํŠธ์—”๋“œ์™€ ๋ฐฑ์—”๋“œ ์‚ฌ์ด

HTTP ์ƒํƒœ ์ฝ”๋“œ๋Š” ํ”„๋ก ํŠธ์—”๋“œ์—์„œ ๋ฐฑ์—”๋“œ๋กœ ๋ณด๋ƒˆ๋˜ ์š”์ฒญ์˜ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ๋ฅผ ์˜๋ฏธํ•˜๋Š” ์ผ์ข…์˜ ์•ฝ์†์ด๋ฉฐ, API๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ํ•ต์‹ฌ ์š”์†Œ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์ƒํƒœ ์ฝ”๋“œ์™€ ๊ด€๋ จํ•˜์—ฌ, ๋ฐฑ์—”๋“œ๋Š” ์ž˜ ๋ชจ๋ฅด๋Š” ํ”„๋ก ํŠธ์—”๋“œ์˜ ์Šฌํ”ˆ ์‚ฌ์ •์ด ์žˆ์Šต๋‹ˆ๋‹ค.์•„๋ž˜๋Š” ์š”์ฒญ์ด ์‹คํŒจํ–ˆ์Œ์—๋„, ๋ฐฑ์—”๋“œ์—์„œ ์ƒํƒœ ์ฝ”๋“œ

JWT

ํ† ํฐ ๊ด€๋ฆฌ ๋ฐฉ์‹ ํ†บ์•„๋ณด๊ธฐ

0. ๋“ค์–ด๊ฐ€๋ฉฐ ๐ŸŽฏ ์„œ๋น„์Šค์— ์ ‘๊ทผํ•˜๋ ค๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋ˆ„๊ตฌ์ธ์ง€ ํ™•์ธํ•˜๋Š” ๊ณผ์ •์„ ์‚ฌ์šฉ์ž ์ธ์ฆ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ฃผ์–ด์ง„ ๊ถŒํ•œ์„ ํ™•์ธํ•˜๋Š” ์ž‘์—…์€ ์ธ๊ฐ€๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ์ธ๊ฐ€๋Š” ๋‹ค๋ฃจ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์ธ์ฆ์—๋Š” ๋งŽ์€ ๋ฐฉ์‹์ด ์žˆ์ง€๋งŒ, ์˜ค๋Š˜์€ ์„ธ์…˜ ์ธ์ฆ ๋ฐฉ

A2A

A2A / MCP ๋ฉ€ํ‹ฐ ์—์ด์ „ํŠธ ์˜ค์ผ€์ŠคํŠธ๋ ˆ์ด์…˜

0. ๋“ค์–ด๊ฐ€๋ฉฐ โœ๏ธ Google for Developers์—, ๋ ˆ์Šคํ† ๋ž‘ ๊ณต๊ธ‰๋ง ์‹œ๋‚˜๋ฆฌ์˜ค๋กœ ์—ฎ์€ 6๋Œ€ ํ”„๋กœํ† ์ฝœ(MCP, A2A, UCP, AP2, A2UI, AG-UI)์— ๋Œ€ํ•œ ๊ฐ€์ด๋“œ๊ฐ€ ๊ฒŒ์‹œ๋œ ์ดํ›„, MCP์™€ A2A๋ถ€ํ„ฐ ๊ตฌํ˜„ํ•ด ๋ณด๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ