FE RYAN
완벽하지 않으면 어때
FE RYAN
전체 방문자
오늘
어제

블로그 메뉴

  • 💾 깃허브 링크
  • 홈
  • 태그
  • 분류 전체보기 (151)
    • 개인프로젝트 (8)
      • 개인 포트폴리오 웹앱 (6)
      • 프론트엔드 기술면접 아카이빙 웹앱 (2)
    • 기록 (121)
      • 원티드 프리온보딩 인턴십 (0)
      • 코드스테이츠 프론트엔드 (75)
      • 생각들 (3)
      • Today I learned (32)
      • 회고 (9)
      • 리뷰 (1)
    • 개발 (17)
      • React (3)
      • Javascript (7)
      • CSS (1)
      • HTML (3)
      • HTTP (1)
      • 자료구조 (0)
      • 알고리즘 (2)
    • 코딩테스트 (2)
      • 백준 (2)
      • 프로그래머스 (0)
    • 디자인 (1)
      • UI & UX (1)
    • 수학 (0)
    • 자기계발 (0)

공지사항

인기 글

태그

  • 프론트엔드
  • 자바스크립트 딥다이브
  • useMemo
  • 자바스크립트
  • Til
  • HTML
  • 메인프로젝트
  • 부트캠프
  • ES6
  • 원시타입
  • css
  • 리액트
  • 타입스크립트
  • 딥다이브
  • 회고
  • seb39
  • 신입개발자
  • seb 39
  • 포트폴리오
  • 코드스테이츠

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
FE RYAN
기록/Today I learned

22.11.12 토 TIL

기록/Today I learned

22.11.12 토 TIL

2022. 11. 12. 23:06
728x90

1. Done


Reference

코딩일레븐 님의 몇 줄로 끝내는 인터랙티브 웹 개발 노하우 [초급편] 강의를 수강하고 학습 내용을 정리합니다.

개념 설명

mousemove DOM 이벤트로 마우스의 수평(X), 수직(Y) 좌표를 clientX , clientY 로 받을 수 있다.

이 값으로 css의 transform 속성을 사용해 translate 해줄 px 값으로 사용한다.

리액트 버젼 코드


  • 강사님 코드
    1. useState
      1. x축과 y축 값을 프로퍼티로 가진 객체를 초기값 0, 0으로 state 초기값 지정
    2. useEffect
      1. clientX, Y 값으로 setState 함수를 변수화하고 mousemove 이벤트에 addEventListener 콜백함수로 사용.
      2. 리턴값으로 클린업하는 함수에 removeEventListener의 콜백으로 윗 단계에서 변수화한 함수 사용.
    const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 });
    
        useEffect(() => {
            const positionSet = (e) => setMousePosition({ x: e.clientX, y: e.clientY });
            window.addEventListener("mousemove", positionSet);
    
            return () => {
                window.removeEventListener("mousemove", positionSet);
            };
        }, []);
    
        return (
            <>
                <p>
                    x: {mousePosition.x} y: {mousePosition.y}
                </p>
            </>
        );
    

[디버깅] Over 200 classes were generated for component styled.div

warning 메세지:

Over 200 classes were generated for component styled.section.
Consider using the attrs method,
together with a style object for frequently changed styles.

x축, y축 값의 state를 props로 받아서 translate의 px값으로 사용하니 마우스 위치 이동시마다 위 컴포넌트가 재렌더링이 일어났다. attrs를 사용하는 방식으로 바꿔보았으나 계속 문제가 지속되어 transform 속성만 인라인 스타일링으로 따로 빼두니 해당 워닝은 해결되었다.

문제 코드

const CursorDiv = styled.div`
  position: absolute;
  width: 100px;
  height: 100px;
  background: linear-gradient(to left, skyblue, blue);
  border-radius: 70%;
  top: 0;
  left: 0;
  transform: translate(${({ position }) => `${position.x}px, ${position.y}px`});
`;

해결: 동적 스타일링하는 속성만 인라인 스타일로 분리.

  • 스타일드 컴포넌트는 정적이거나 변화가 많지 않은 스타일이 적용되는 컴포넌트에만 사용해야 한다.
  • 애니메이션 처럼 빈번히 변화하는 스타일에는 인라인 스타일을 사용해라.
<CursorDiv
  style={{
    transform: `translate(${cursorPosition.x}px, ${cursorPosition.y}px)`,
  }}
/>

Reference

  • https://velog.io/@ayaan92/styled-components-.attrs에-대하여
  • styled-components 공식문서

내 코드 - jsx

import React, { useState } from 'react';
import GlobalStyle from './GlobalStyle';
import styled from 'styled-components';

function App() {
  const [cursorPosition, setCursorPosition] = useState({
    x: 0,
    y: 0,
  });
  const positionSetter = e => {
    return setCursorPosition({
      x: e.clientX,
      y: e.clientY,
    });
  };

  return (
    <>
      <GlobalStyle />
      <Main onMouseMove={positionSetter}>
        <H1>
          x: {cursorPosition.x} y: {cursorPosition.y}
        </H1>
        <CursorDiv
          style={{
            transform: `translate(${cursorPosition.x}px, ${cursorPosition.y}px)`,
          }}
        />
      </Main>
    </>
  );
}

export default App;

내 코드 - styled-components

const Main = styled.main`
  width: 100vw;
  height: 100vh;
`;

const H1 = styled.h1`
  color: #ffffff;
`;

const CursorDiv = styled.div`
  position: absolute;
  width: 100px;
  height: 100px;
  background: linear-gradient(to left, skyblue, blue);
  border-radius: 70%;
  top: 0;
  left: 0;
`;

바닐라JS 버젼 코드


window.onload()

html 문서가 다 준비된 후에 자바스크립트 코드가 실행되도록 지시하는 함수. 리액트에서는 useEffect 훅을 사용하면 된다.

script.js

window.onload = () => {
  const h1 = document.getElementsByTagName('h1')[0];
  //   const cursor_item = document.querySelector('cursor_item');
  const cursor_item = document.getElementsByClassName('cursor_item')[0];
  const mouseFunc = e => {
    h1.innerHTML = `clientX: ${e.clientX} clientY: ${e.clientY}`;
    cursor_item.style.transform =
      'translate(' + e.clientX + 'px,' + e.clientY + 'px)';
    // translate(635px,286px)
  };
  window.addEventListener('mousemove', mouseFunc, false);
};

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./styles.css" />
    <script src="./script.js" defer></script>
  </head>
  <body>
    <h1>테스트</h1>
    <div class="cursor_item"></div>
  </body>
</html>

style.css

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  position: relative;
  background-color: #000000;
}

h1 {
  color: #ffffff;
}

.cursor_item {
  position: absolute;
  width: 100px;
  height: 100px;
  background-color: red;
  top: 0;
  left: 0;
}
728x90
저작자표시 비영리 변경금지 (새창열림)

'기록 > Today I learned' 카테고리의 다른 글

22.11.11 금 TIL  (0) 2022.11.12
22.11.10 목 TIL  (0) 2022.11.10
22.10.27 TIL - 포폴앱 타입스크립트 마이그레이션  (0) 2022.10.29
22.10.26 TIL  (0) 2022.10.26
22.10.25 TIL  (0) 2022.10.25
  • 1. Done
  • 개념 설명
  • 리액트 버젼 코드
  • [디버깅] Over 200 classes were generated for component styled.div
  • 바닐라JS 버젼 코드
'기록/Today I learned' 카테고리의 다른 글
  • 22.11.11 금 TIL
  • 22.11.10 목 TIL
  • 22.10.27 TIL - 포폴앱 타입스크립트 마이그레이션
  • 22.10.26 TIL
FE RYAN
FE RYAN

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.