강의

멘토링

로드맵

Inflearn brand logo image

인프런 커뮤니티 질문&답변

dsfsdf님의 프로필 이미지
dsfsdf

작성한 질문수

[리뉴얼] React로 NodeBird SNS 만들기

게시글 수정하기

useMemo 질문이요

작성

·

257

0

const TextAreaStyle = {
  resize: 'none',
  height: 120,
  marginBotton: 5,
};

function EditPostCard() {
  return (
    <>
      <TextArea rows={4} style={TextAreaStyle} />
      <Space wrap>
        <Button type="primary">수정</Button>
        <Button type="primary" danger>취소</Button>
      </Space>
    </>
  );
}

export default EditPostCard;

컴포넌트 밖에서 스타일객체를 선언한 경우


function EditPostCard() {
  const TextAreaStyle = useMemo(() => ({
    resize: 'none',
    height: 120,
    marginBotton: 5,
  }));

  return (
    <>
      <TextArea rows={4} style={TextAreaStyle} />
      <Space wrap>
        <Button type="primary">수정</Button>
        <Button type="primary" danger>취소</Button>
      </Space>
    </>
  );
}

컴포넌트 안에서 useMemo를 사용한 경우

리렌더링때문에 스타일을 따로 선언해주었는데요.

두 가지 경우가 큰 차이가 있나요? useMemo안 쓰고

컴포넌트 밖에다 선언해줘도 상관없나요?

답변 1

0

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

네 원래 컴포넌트 바깥에다가 선언하는게 최고입니다. 부득이하게 안에다 넣는 경우는 useMemo를 사용해서 최적화할 수 있습니다.

dsfsdf님의 프로필 이미지
dsfsdf

작성한 질문수

질문하기