개발자
쟁점은 select component안에 있는 table 요소 내부를 어떤 식으로 react.memo로 component 분리해서 children으로 처리하는게 나을까요?? 아니면 useMemo로 한 컴포넌트 내에서 처리하는게 나을까요??
1// 1번입니다.
2// select-table.tsx
3
4import React, { memo } from "react";
5import { BlockType } from "@package/api-type";
6export interface ISelectTableProps {
7 onClick: (item: string) => void;
8 list: { [Key in BlockType]: string };
9}
10
11export const SelectTable = memo(({ onClick, list }: ISelectTableProps) => {
12 return (
13 <>
14 {Object.entries(list).map((user) => (
15 <div onMouseDown={() => onClick(user[0])} key={user[0]}>
16 {user[1]}
17 </div>
18 ))}
19 </>
20 );
21});
22// --------------------------- //
23// select.tsx
24import styled from "@emotion/styled";
25import { keyframes } from "@emotion/react";
26import React, { useState, useLayoutEffect, MouseEvent, ReactNode } from "react";
27import { PolygonIcon } from "@/assets/polygon-icon";
28export interface ISelectProps {
29 now: string;
30 children: ReactNode;
31}
32
33export const Select = ({ now, children }: ISelectProps) => {
34 const [state, setState] = useState<boolean>(false);
35 useLayoutEffect(() => {
36 document.addEventListener("click", () => {
37 setState(false);
38 });
39 }, [state]);
40 return (
41 <>
42 <_Layout>
43 <_InfoButton
44 id={now}
45 {...{ state }}
46 onClick={(e: MouseEvent<HTMLDivElement>) => {
47 e.stopPropagation();
48 setState((prev) => !prev);
49 }}
50 >
51 <div>{now}</div>
52 <_SelectIcon {...{ state: state }}>
53 <PolygonIcon />
54 </_SelectIcon>
55 </_InfoButton>
56 <_SelectList {...{ state }}>{children}</_SelectList>
57 </_Layout>
58 </>
59 );
60};
61
62// --------------------------- //
63// 2번입니다.
64// select.tsx
65import styled from "@emotion/styled";
66import { keyframes } from "@emotion/react";
67import React, { useState, useLayoutEffect, MouseEvent, useMemo } from "react";
68import { BlockType } from "@package/api-type";
69import { PolygonIcon } from "@/assets/polygon-icon";
70export interface ISelectProps {
71 onClick: (item: string) => void;
72 now: BlockType;
73 list: { [Key in BlockType]: string };
74}
75
76export const Select = ({ onClick, now, list }: ISelectProps) => {
77 const [state, setState] = useState<boolean>(false);
78 useLayoutEffect(() => {
79 document.addEventListener("click", () => {
80 setState(false);
81 });
82 }, [state]);
83 const TableMemo = useMemo(
84 () =>
85 Object.entries(list).map((user) => (
86 <div onMouseDown={() => onClick(user[0])} key={user[0]}>
87 {user[1]}
88 </div>
89 )),
90 [list]
91 );
92 return (
93 <>
94 <_Layout>
95 <_InfoButton
96 id={now}
97 {...{ state }}
98 onClick={(e: MouseEvent<HTMLDivElement>) => {
99 e.stopPropagation();
100 setState((prev) => !prev);
101 }}
102 >
103 <div>{list[now]}</div>
104 <_SelectIcon {...{ state: state }}>
105 <PolygonIcon />
106 </_SelectIcon>
107 </_InfoButton>
108 <_SelectList {...{ state }}>{TableMemo}</_SelectList>
109 </_Layout>
110 </>
111 );
112};
답변 1
두 가지 방법 모두 장단점이 있으며, 선택은 개발자의 선호도와 프로젝트 요구사항에 따라 달라집니다. 다음은 두 방법의 차이점과 고려해야 할 사항입니다: 1번 방법: 컴포넌트 분리하기 (React.memo) 장점: -관심사의 분리: 테이블 요소를 별도의 컴포넌트로 분리하여 관심사를 분리할 수 있습니다. SelectTable 컴포넌트는 테이블의 렌더링 로직에만 집중하게 됩니다. -재사용성: SelectTable 컴포넌트를 다른 곳에서도 재사용할 수 있습니다. 단점: -컴포넌트 개수 증가: 여러 개의 컴포넌트로 분리하면 컴포넌트의 개수가 증가하게 됩니다. 이는 관리해야 할 컴포넌트 수를 늘릴 수 있습니다. 2번 방법: useMemo를 이용하여 컴포넌트 내부에서 처리하기 장점: -컴포넌트 수 감소: 테이블 요소를 컴포넌트로 분리하지 않고 한 컴포넌트 내에서 처리할 수 있습니다. 컴포넌트 수가 줄어들어 개발 및 유지보수가 간편해집니다. 단점: -관심사 혼합: Select 컴포넌트 내에서 테이블 렌더링 로직을 처리하므로 관심사가 혼합될 수 있습니다. -재사용성 감소: 테이블 렌더링 로직이 Select 컴포넌트 내에 포함되므로 다른 곳에서 재사용하기 어려울 수 있습니다. 위의 장단점을 고려하여 프로젝트의 요구사항과 개발 편의성에 따라 선택하면 됩니다. 만약 테이블 요소를 여러 곳에서 재사용할 필요가 있다면 1번 방법이 유리할 수 있고, 테이블 요소가 특정 컴포넌트에서만 사용되는 경우 2번 방법을 고려할 수 있습니다.
커리어리 AI 봇의 답변을 평가해 주세요!
지금 가입하면 모든 질문의 답변을 볼 수 있어요!
현직자들의 명쾌한 답변을 얻을 수 있어요.
이미 회원이신가요?
지금 가입하면 모든 질문의 답변을 볼 수 있어요!