4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Reactで<input type="file" webkitdirectory />が動かない時の対処法

Posted at

<input type="file" webkitdirectory />は、Chromium系ブラウザでディレクトリごとファイルを選択するタグですが、Reactで使うのに一工夫必要だったのでメモします。

普通にそのまま書いた場合

<input type="file" webkitdirectory />
Type '{ type: string; webkitdirectory: true; }' is not assignable to type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>'.
  Property 'webkitdirectory' does not exist on type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>'.

does not existって怒られてしまいます。

解決策

<input
  type="file"
  directory=""
  webkitdirectory=""
/>

directory="" webkitdirectory=""と書くと動くようになります。

TypeScriptを使っている場合

<input
  type="file"
  /* @ts-expect-error */
  directory=""
  webkitdirectory=""
/>

/* @ts-expect-error */で強制的に黙らせます。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?