강의

멘토링

커뮤니티

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

이태경님의 프로필 이미지
이태경

작성한 질문수

타입스크립트로 배우는 리액트(React.js) : 기초부터 최신 기술까지 완벽하게

할일 관리 앱(메모이제이션)

할일 관리 앱(메모이제이션)

작성

·

15

0

import { useId } from "react";

type CheckboxProps = Omit<React.ComponentPropsWithoutRef<"input">, "type"> & {
  type?: "checkbox";
  parentClassName: string;
};
export default function Checkbox(props: CheckboxProps) {
  const uuid = useId();
  const { parentClassName, children, ...rest } = props;
  return (
    <>
      <div className={parentClassName}>
        <input id={uuid} {...rest} />
        <label htmlFor={uuid}>{children}</label>
      </div>
    </>
  );
}

 

useEffect(() => {
    const randomText = Array.from(
      { length: 100 },
      (_, index) => `Todo Item #${index + 1}`,
    );

    randomText.forEach((text) => addTodo(text));
  }, []);

대량 데이터 생성 후 테스트를 하는데 중복 키값이 생성되었는데요. uuid 부분인 것 같은데. 노트북 장비 사양 때문에 그런건지요?

 

installHook.js:1 Encountered two children with the same key, 1770775434402. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

답변 2

2

해당 코드에서 문제가 아니라 todoList 를 map으로 돌릴 때 key 값이 중복된다는 메세지이니

반복되는 컴포넌트 요소에 key 프롭스를 어떻게 주었는지 확인해 봐야 합니다.

// 아래와 같은 코드 내에서 key에 할당한 값이 중복되는 지를 확인해 보세요.
{todos.map((todo, index) => (  
    <div key={index}>{todo}</div>
))}
수코딩님의 프로필 이미지
수코딩
지식공유자

🙏

0

수코딩님의 프로필 이미지
수코딩
지식공유자

안녕하세요. 수코딩입니다.

Codingbear님의 답변 내용이 맞습니다.

해당 부분이 아니라 map() 메서드처럼 반복 렌더링 하는 부분의 코드가

어떻게 되어있는지 확인해봐야 합니다.

sucoding@naver.com으로 전체 코드를 압축해서 보내주셔도 됩니다.

감사합니다!

이태경님의 프로필 이미지
이태경

작성한 질문수

질문하기