typeScript에서 props사용하는 법

이무헌·2022년 10월 24일
0

react

목록 보기
6/19
<Table studentList={studentList} />

테이블 컴포넌트에 studentList를 props로 전달하는 아주 좋은 함수다

export interface studentList {
    attrClass:string,
    birth: string,
    gender:string,
    name: string,
    recent: string,
    uid: string,
    _id: string
  }
  

타입은 다음과 같이 정했다.
이제 받기만 하면 되겠지?

const Table= ({studentList}:studentList) => {...}

당연히 안된다. 왜 안될까?

//부모 컴포넌트에서 뜬 오류
'{ studentList: studentList; }' 형식은 'IntrinsicAttributes & studentList' 형식에 할당할 수 없습니다.
  'IntrinsicAttributes & studentList' 형식에 'studentList' 속성이 없습니다.ts(2322)
//자식 컴포넌트에서 뜬 오류
'studentList'이(가) 선언은 되었지만 해당 값이 읽히지는 않았습니다.ts(6133

인터넷을 뒤져본 결과 자식에서 잘 못 받아버렸다.

const Table= ({studentList}:{studentList:studentList}) => {...}

객체 디스트럭팅은 이렇게 해야한다고 한다... props로 넘겨준 객체를 비구조화로 선언하고 그 안에서 다시 한 번 객체를 선언해야 한다는게 이해가 되지 않는다... 좀 더 찾아봐야겠다.

profile
개발당시에 직면한 이슈를 정리하는 곳

0개의 댓글