React upload hooks and components
The React upload experience turns a typed UploadThing route into a file picker, drop area, or custom upload flow. It combines route limits with local file selection, reports upload progress, and exposes clear callbacks for completion, errors, and cancellation.
React upload surface
The package offers a low-level hook for custom interfaces and ready-made components for common file-selection patterns. The generated helpers bind the UploadThing API URL and file-router type once, so each screen can name an endpoint without repeating transport setup. See the React SDK page for the wider client API.
Typed upload hook
generateReactHelpers creates a typed useUploadThing hook for a file router. The hook returns startUpload, isUploading, and the selected endpoint's route configuration. A custom interface can use these values to own its file list and visual state while still using the same upload lifecycle as the built-in components.
The hook accepts callbacks around the upload. onBeforeUploadBegin can replace or rename the files before the request starts. onUploadBegin marks the start of each file. onUploadProgress reports aggregate progress. Completion and error callbacks receive typed results or typed UploadThing errors. An AbortSignal gives a custom interface control over cancellation.
References: packages/…/use-uploadthing.ts• L52-119 packages/…/use-uploadthing.ts• L160-205 packages/…/index.ts• L1-15
Ready-made upload components
UploadButton presents a compact picker. It can upload as soon as the user selects files or hold the selection for a second click. UploadDropzone adds drag and drop, keyboard access, and optional paste support. Uploader places both ready-made choices in one composed view and is mainly useful as a simple showcase or broad default.
Typed factory helpers such as generateUploadButton, generateUploadDropzone, and generateUploader return components bound to a file-router type. They also bind the API URL and fetch function, which keeps endpoint props focused on the upload experience.
References: packages/…/button.tsx• L52-93 packages/…/dropzone.tsx• L82-128 packages/…/uploader.tsx• L10-35 packages/…/index.tsx• L19-95
File selection modes
The components use the browser file picker as their base input. The button and dropzone then add different ways to collect files before they pass an accepted selection to the upload hook.
Button selection
The button wraps a hidden file input in a visible label. In auto mode, selection starts the upload at once. In manual mode, selection changes the label to an upload action and shows a clear action; the next click starts the upload instead of opening a new picker.
The component reports selected files through onChange. Clearing the manual selection reports an empty list and resets the browser input, so the user can select the same file again. If paste support is enabled and the file input is focused, pasted files join the current selection.
References: packages/…/button.tsx• L99-223 packages/…/button.tsx• L270-347 packages/…/types.ts• L184-190
Dropzone interaction
The dropzone makes its root area both a drag target and a keyboard-accessible file picker. Users can click the area, press Enter or Space, drag files into it, or paste files when appendOnPaste is enabled and the dropzone has focus. A drop replaces the current list. A paste appends to it.
The useDropzone hook is also available for fully custom markup. It returns root and input prop builders plus drag, focus, dialog, and accepted-file state. The custom markup must attach those props to preserve the picker and drop behavior.
The following flow shows how a dropzone turns a user action into either a stored selection or an upload.
flowchart LR
A[Click, key, drop, or paste] --> B[Read File objects]
B --> C[Check type, size, and count]
C --> D[onChange with accepted files]
D --> E{Mode}
E -->|auto| F[Start upload]
E -->|manual| G[Wait for Upload click]
G --> F
References: packages/…/dropzone.tsx• L215-254 packages/…/dropzone.tsx• L427-727 packages/…/usePaste.ts• L5-17
Route metadata and validation
The upload controls learn their allowed file types and limits from endpoint metadata. This keeps the picker text, browser input hints, drop behavior, and visible allowed-content label aligned with the selected file route.
Route constraints
An endpoint's expanded route configuration describes each permitted file type and its size and count limits. The components use this metadata to decide whether multiple files are allowed, build the input's MIME filter, and show text such as the permitted type and maximum size.
The dropzone applies the type, minimum-size, maximum-size, and quantity checks when files arrive. A selection that fails the checks becomes an empty accepted list rather than an upload. Drag-over state also indicates whether the files currently being dragged meet the same rules. The browser accept attribute helps guide picker choices, but it is not a substitute for the dropzone checks.
References: packages/…/types.ts• L139-177 packages/…/types.ts• L223-234 packages/…/component-utils.ts• L28-80 packages/…/dropzone-utils.ts• L63-116 packages/…/dropzone.tsx• L578-624
Route metadata loading
When route metadata is not already present, the hook fetches it from the configured UploadThing endpoint and caches the result for that hook instance. Next.js can provide the same metadata during rendering through NextSSRPlugin; the client then reads the embedded value and avoids the metadata request. This path matters when the control must become ready without a client-side route-config fetch. See React and Next.js integration for the framework setup.
References: packages/…/use-uploadthing.ts• L38-50 packages/…/use-uploadthing.ts• L147-154 packages/…/useFetch.ts• L20-98 packages/…/next-ssr-plugin.tsx• L12-40
Visual and text customization
The components provide a default visual language while exposing stable parts for application styling. A class name changes the outer control, while appearance and content fields target the named control parts without requiring a fork.
Appearance and content
Appearance fields can be class names, CSS objects, or functions that choose either form from the current control context. The context includes readiness, upload state, progress, allowed file types, and the current files. Button and dropzone contexts also include their own interaction details, such as active dragging for the dropzone.
Content fields replace default labels or icons with text or React elements. This supports product-specific copy while preserving the component's state changes. A custom cn function can merge class lists when an application uses a class merger with ordering rules.
The package also exposes its generated stylesheet entry point. Its base source supplies Tailwind layers, while component classes provide the default layout, focus, disabled, drag, and progress treatment.
References: packages/…/button.tsx• L30-68 packages/…/dropzone.tsx• L57-105 packages/…/component-utils.ts• L151-214 packages/…/styles.css• L1-3 packages/…/package.json• L15-18
Upload lifecycle
The controls represent both metadata readiness and upload activity. They keep the selected files local to the component, while the shared hook owns the request and progress callbacks.
State transitions
The visible component state is one of readying, ready, uploading, or disabled. It starts in readying until route metadata supplies permitted file types. It then becomes ready unless the control is disabled. A started upload changes it to uploading, and completion or failure returns it to the non-uploading state.
Progress is tracked per file and exposed as one aggregate percentage. The default report is coarse, in ten-percent steps; callers can request fine, all-event, or another supported granularity. During upload, the button shows progress and a cancel affordance. The dropzone also renders progress as a fill across its upload button.
The state relationship is summarized below.
stateDiagram-v2
[*] --> Readying
Readying --> Ready: route metadata available
Ready --> Disabled: disabled prop
Disabled --> Ready: enabled and metadata available
Ready --> Uploading: auto selection or upload click
Uploading --> Ready: complete or error
Uploading --> Ready: abort
References: packages/…/button.tsx• L137-148 packages/…/dropzone.tsx• L169-182 packages/…/use-uploadthing.ts• L73-87 packages/…/use-uploadthing.ts• L94-144
Completion and cancellation
On successful completion, the ready-made controls clear their selected files and reset progress before they call the consumer completion callback. The hook calls that callback after the server completion data when the route waits for server data. A failed upload calls the typed error callback and leaves the component ready for another attempt.
Clicking the active upload control aborts its request. The ready-made components own the abort controller and call onUploadAborted when the hook reports an abort. A custom interface should pass its own signal to useUploadThing when it needs more control than the built-in cancel action provides.
References: packages/…/types.ts• L95-119 packages/…/types.ts• L135-140 packages/…/button.tsx• L112-179 packages/…/dropzone.tsx• L147-213 packages/…/use-uploadthing.ts• L119-144
Change checklist
When these files or sections are changed, remember to consider these:
- Keep route metadata, browser MIME hints, dropzone checks, and allowed-content text aligned when the route-config shape changes.
- Keep
UploadthingComponentPropsand the generated component factories aligned so endpoint input types and shared transport options remain available in both direct and generated usage. - Keep the state names and
data-stateattributes aligned with appearance callbacks and custom CSS. A renamed state can silently remove visual feedback. - Keep abort behavior aligned between the hook's
AbortSignalcontract and the component-ownedonUploadAbortedcallback. - Keep the exported stylesheet path aligned with the package build when default component styling changes.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Route metadata is missing or cannot load | The control stays in readying and cannot offer a reliable file choice | Make the API URL reachable or provide route metadata through the framework integration path |
| A drop violates type, size, or count rules | The accepted list is empty and auto mode has nothing to upload | Use the route's displayed limits and handle onChange as an accepted-file event |
| A manual selection is treated as an upload event | The UI can show selected files while no request starts until the upload click | Use config.mode: "manual" when the flow needs review or clear actions |
A custom layout omits props from useDropzone | Click, keyboard, drag, or input behavior is lost | Apply both getRootProps() and getInputProps() to the matching elements |
| An upload is aborted without handling the abort callback | The UI can miss the distinction between cancellation and an upload error | Handle onUploadAborted on ready-made components or inspect UploadAbortedError in custom flows |
Key files
| File | Why to read it |
|---|---|
| packages/…/use-uploadthing.ts | Defines the shared typed upload hook, route metadata lookup, progress, callbacks, and abort behavior. |
| packages/…/button.tsx | Shows the compact picker, auto and manual modes, selection clearing, and state-driven content. |
| packages/…/dropzone.tsx | Shows drag and drop, keyboard access, paste handling, validation entry, and dropzone rendering. |
| packages/…/index.tsx | Shows the public component exports and typed factory helpers. |
| packages/…/types.ts | Defines the common component contract and upload lifecycle callbacks. |
| packages/…/dropzone-utils.ts | Defines file acceptance, size and quantity checks, and the low-level dropzone state. |
| packages/…/component-utils.ts | Defines route-derived labels and the shared styling and content model. |
| packages/…/next-ssr-plugin.tsx | Shows how Next.js can provide route metadata before the controls render. |