useFile

Manages file selection state and provides input-ready props for uploads and drag-and-drop.

Tip

To allow users to select multiple files, ensure you pass the multiple: true option. Otherwise, the hook will default to a single-file selection.

Note

This hook provides the state and logic for file handling but does not perform the actual upload. You should use the files array to build a FormData object for your API calls.

Installation

bash

Description

An unopinionated hook for handling file inputs. It abstracts the boilerplate of managing File[] states and provides a spreadable inputProps object to keep UI components clean while supporting both single and multiple file selections.

Parameters

NameTypeDescription
options.acceptstringFile types accepted (e.g., '.png, .pdf'). Default: undefined.
options.disabledbooleanDisables the file input. Default: false.
options.multiplebooleanAllows selecting more than one file. Default: false.

Return values

NameTypeDescription
filesFile[]Array of currently selected File objects.
hasFilesbooleanUtility boolean that is true if at least one file is selected.
inputPropsReact.InputHTMLAttributes<HTMLInputElement>Props to be spread onto a <input type='file' /> element.
reset() => voidFunction to clear the current selection.
setFiles(files: File[] | FileList) => voidManual setter for files (ideal for Drag & Drop implementations).

Demo

File Upload

List of selected filesNo files selected.

Source code

tsx