To use the webkitdirectory attribute in TypeScript and React, you can create a file input element and set the webkitdirectory attribute to allow users to select an entire directory instead of individual files. The webkitdirectory attribute is specific to some browsers like Chrome and allows you to access all files within a selected directory. Here's how you can do it in a TypeScript and React application:

  1. Create a React component that includes the file input element with the webkitdirectory attribute:
import React from 'react'; interface FileSelectorProps { onFilesSelected: (files: FileList | null) => void; } const FileSelector: React.FC<FileSelectorProps> = ({ onFilesSelected }) => { const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => { const files =; onFilesSelected(files); }; return ( <div> <input type="file" onChange={handleFileChange} webkitdirectory="true" /> </div> ); }; export default FileSelector;
  1. Use the FileSelector component in your main application:
import React from 'react'; import FileSelector from './FileSelector'; const App: React.FC = () => { const handleFilesSelected = (files: FileList | null) => { if (files) { // Process the selected files here console.log(files); } }; return ( <div> <h1>File Selector Example</h1> <FileSelector onFilesSelected={handleFilesSelected} /> </div> ); }; export default App;

In this example, the FileSelector component accepts a prop onFilesSelected, which is a callback function that will be triggered when the user selects files. The selected files are then passed back to the main application for further processing.

Please note that the webkitdirectory attribute is specific to WebKit-based browsers (e.g., Chrome, Safari), and it may not be supported in all browsers. Other browsers may have different ways to enable directory selection for file inputs. To provide cross-browser support, you may consider using a library like react-dropzone that abstracts these differences and provides a consistent interface for file selection across browsers.

Have questions or queries?
Get in Touch