타입스크립트 FileList 배열로 변경

Lipton·2023년 1월 26일
0

TypeScript

목록 보기
5/6
  • 리액트 프로젝트
  /** 파일 목록을 배열로 전환 */
  const handleChangeFile = (e: React.ChangeEvent<HTMLInputElement>) => {
    console.log(e.target.files);

	// 배열로 변환되는 코드
    const fileArray = Array.prototype.slice.call(e.target.files);
    console.log(fileArray);
  };

위에 일반 e.target.files. FileList 타입으로 반복문 사용 불가
아래 배열로 변환한 fileArray배열. 배열로 반복문 사용가능

또는 Array.from(e.currentTarget.files || []) 하면 배열로 변환되어 동일하게 나옴

profile
Web Frontend

0개의 댓글