Overview
Uploadcare File Uploader is built with Web Components, making it compatible with React applications. This guide shows you how to integrate the uploader into your React projects using TypeScript.Installation
Install the File Uploader package from npm:Basic Setup
Step 1: Import and Define Components
Create a component to initialize the File Uploader:App.tsx
Replace
YOUR_PUBLIC_KEY with your actual Uploadcare public key from your dashboard.Step 2: TypeScript Support
To enable TypeScript support and JSX types for the Web Components, add the following to your component file or a types declaration file:types.d.ts
tsconfig.json:
tsconfig.json
Available Solutions
The File Uploader provides three pre-built solutions:- Regular
- Inline
- Minimal
Full-featured uploader with modal dialog:Best for:
- Standard file upload workflows
- Apps needing a modal interface
- Multiple file uploads with preview
Configuration
All configuration is managed through theuc-config component. Here are common options:
Key Configuration Props
| Prop | Type | Description |
|---|---|---|
ctx-name | string | Context name to link uploader and config |
pubkey | string | Your Uploadcare public key |
multiple | boolean | Allow multiple file selection |
img-only | boolean | Restrict uploads to images only |
source-list | string | Comma-separated list of upload sources |
max-local-file-size-bytes | number | Maximum file size in bytes |
For a complete list of configuration options, see the Configuration Reference.
Handling Upload Events
Listen to upload events using refs and event listeners:FileUploadComponent.tsx
Form Integration
Integrate the uploader with React forms:FormWithUpload.tsx
Custom Styling
Customize the uploader appearance using CSS variables:App.css
Reusable Hook
Create a custom hook for cleaner integration:useFileUploader.ts
Best Practices
Initialize once
Call
defineComponents() only once during the app lifecycle, typically in a useEffect with empty dependencies or at the root level.Use unique context names
If you have multiple uploaders on the same page, use different
ctx-name values for each.Clean up event listeners
Always remove event listeners in the cleanup function of
useEffect to prevent memory leaks.Troubleshooting
TypeScript errors with JSX
If you see TypeScript errors about unknown elements, ensure you’ve added the JSX types reference:Components not rendering
Make sure you’ve calleddefineComponents(UC) before rendering the components in the DOM.
CSS not loading
Ensure you’ve imported the CSS file:Next Steps
Configuration
Explore all configuration options
Events API
Learn about available events
Live Examples
View complete React examples
Next.js Integration
Learn about Next.js-specific considerations