Guia de Migração
Como migrar para o Dropup de outras bibliotecas de upload populares.
De react-dropzone
react-dropzone é uma biblioteca popular de arrastar e soltar. O Dropup fornece funcionalidade similar com suporte integrado a upload.
Antes (react-dropzone)
import { useDropzone } from 'react-dropzone';
function OldUploader() {
const [files, setFiles] = useState([]);
const { getRootProps, getInputProps, isDragActive } = useDropzone({
accept: { 'image/*': [] },
maxSize: 10485760,
onDrop: (acceptedFiles) => {
setFiles(acceptedFiles.map(file => ({
...file,
preview: URL.createObjectURL(file),
})));
},
});
const handleUpload = async () => {
for (const file of files) {
const formData = new FormData();
formData.append('file', file);
await fetch('/api/upload', {
method: 'POST',
body: formData,
});
}
};
return (
<div {...getRootProps()}>
<input {...getInputProps()} />
{isDragActive ? <p>Solte aqui</p> : <p>Arraste arquivos aqui</p>}
</div>
);
}
Depois (Dropup)
import { useDropup } from '@samithahansaka/dropup';
function NewUploader() {
const { files, actions, state, getDropProps, getInputProps } = useDropup({
accept: 'image/*',
maxSize: 10 * 1024 * 1024,
upload: { url: '/api/upload' }, // Upload integrado!
});
return (
<div {...getDropProps()}>
<input {...getInputProps()} />
{state.isDragging ? <p>Solte aqui</p> : <p>Arraste arquivos aqui</p>}
{files.map(file => (
<div key={file.id}>
{file.preview && <img src={file.preview} />}
<span>{file.progress}%</span>
</div>
))}
<button onClick={() => actions.upload()}>Enviar</button>
</div>
);
}
Principais Diferenças
| react-dropzone | Dropup |
|---|---|
getRootProps() | getDropProps() |
isDragActive | state.isDragging |
accept: { 'image/*': [] } | accept: 'image/*' |
| Sem suporte a upload | Upload integrado com progresso |
| Limpeza manual de preview | Limpeza automática |
De react-uploady
react-uploady é uma biblioteca de upload rica em recursos com múltiplos componentes.
Antes (react-uploady)
import Uploady, { useItemProgressListener, useUploady } from '@rpldy/uploady';
import UploadDropZone from '@rpldy/upload-drop-zone';
function OldApp() {
return (
<Uploady destination={{ url: '/api/upload' }}>
<UploadDropZone onDragOverClassName="drag-over">
<div>Solte arquivos aqui</div>
</UploadDropZone>
<UploadProgress />
</Uploady>
);
}
function UploadProgress() {
useItemProgressListener((item) => {
console.log(`${item.file.name}: ${item.completed}%`);
});
const { processPending } = useUploady();
return <button onClick={processPending}>Enviar</button>;
}
Depois (Dropup)
import { useDropup } from '@samithahansaka/dropup';
function NewApp() {
const { files, actions, getDropProps, getInputProps } = useDropup({
upload: { url: '/api/upload' },
onUploadProgress: (file, progress) => {
console.log(`${file.name}: ${progress}%`);
},
});
return (
<div {...getDropProps()}>
<input {...getInputProps()} />
<div>Solte arquivos aqui</div>
{files.map(file => (
<div key={file.id}>
{file.name}: {file.progress}%
</div>
))}
<button onClick={() => actions.upload()}>Enviar</button>
</div>
);
}
Principais Diferenças
| react-uploady | Dropup |
|---|---|
| Provider + Componentes | Hook único |
useItemProgressListener | Callback onUploadProgress |
processPending() | actions.upload() |
| Múltiplos pacotes | Tudo em um |
De react-dropzone-uploader
react-dropzone-uploader combina dropzone com funcionalidade de upload.
Antes (react-dropzone-uploader)
import Dropzone from 'react-dropzone-uploader';
import 'react-dropzone-uploader/dist/styles.css';
function OldUploader() {
const getUploadParams = () => ({ url: '/api/upload' });
const handleChangeStatus = ({ meta, file }, status) => {
console.log(status, meta, file);
};
return (
<Dropzone
getUploadParams={getUploadParams}
onChangeStatus={handleChangeStatus}
accept="image/*"
maxSizeBytes={10485760}
/>
);
}
Depois (Dropup)
import { useDropup } from '@samithahansaka/dropup';
function NewUploader() {
const { files, getDropProps, getInputProps } = useDropup({
accept: 'image/*',
maxSize: 10 * 1024 * 1024,
upload: { url: '/api/upload' },
autoUpload: true,
onUploadStart: (file) => console.log('enviando', file),
onUploadComplete: (file) => console.log('concluído', file),
onUploadError: (file, err) => console.log('erro', file, err),
});
return (
<div {...getDropProps()}>
<input {...getInputProps()} />
{/* Sua UI personalizada */}
</div>
);
}
Principais Diferenças
| react-dropzone-uploader | Dropup |
|---|---|
| Componente com UI integrada | Headless (traga sua própria UI) |
getUploadParams | Opção upload |
onChangeStatus | Callbacks específicos |
| Importação de CSS necessária | Sem estilos |
De Uppy
Uppy é um kit de ferramentas de upload completo.
Antes (Uppy)
import Uppy from '@uppy/core';
import Dashboard from '@uppy/dashboard';
import XHRUpload from '@uppy/xhr-upload';
import '@uppy/core/dist/style.css';
import '@uppy/dashboard/dist/style.css';
const uppy = new Uppy()
.use(Dashboard, { inline: true, target: '#uppy' })
.use(XHRUpload, { endpoint: '/api/upload' });
uppy.on('upload-success', (file, response) => {
console.log('Sucesso:', file, response);
});
function OldUploader() {
return <div id="uppy" />;
}
Depois (Dropup)
import { useDropup } from '@samithahansaka/dropup';
function NewUploader() {
const { files, actions, state, getDropProps, getInputProps } = useDropup({
upload: { url: '/api/upload' },
onUploadComplete: (file, response) => {
console.log('Sucesso:', file, response);
},
});
return (
<div>
<div {...getDropProps()}>
<input {...getInputProps()} />
Solte arquivos aqui
</div>
{files.map(file => (
<div key={file.id}>
{file.preview && <img src={file.preview} />}
<span>{file.name}</span>
<span>{file.progress}%</span>
</div>
))}
<button onClick={() => actions.upload()}>Enviar</button>
</div>
);
}
Principais Diferenças
| Uppy | Dropup |
|---|---|
| UI completa incluída | Headless |
| Sistema de plugins | Tudo em um |
| Bundle maior | < 10KB |
| API imperativa | React hooks |
Passos Comuns de Migração
1. Instalar Dropup
npm uninstall react-dropzone react-uploady @rpldy/uploady # Remover antigo
npm install @samithahansaka/dropup
2. Atualizar Imports
// Antes
import { useDropzone } from 'react-dropzone';
// Depois
import { useDropup } from '@samithahansaka/dropup';
3. Atualizar Uso do Hook
// Antes
const { getRootProps, getInputProps, isDragActive } = useDropzone({...});
// Depois
const { getDropProps, getInputProps, state } = useDropup({...});
// Use state.isDragging em vez de isDragActive
4. Adicionar Configuração de Upload
// Dropup inclui funcionalidade de upload
useDropup({
upload: { url: '/api/upload' },
});
5. Atualizar Manipuladores de Eventos
// Antes (react-dropzone)
onDrop: (files) => {...}
// Depois (Dropup)
onFilesAdded: (files) => {...}
onUploadComplete: (file) => {...}
6. Atualizar Template
// Antes
<div {...getRootProps()}>
<input {...getInputProps()} />
</div>
// Depois
<div {...getDropProps()}>
<input {...getInputProps()} />
</div>
Comparação de Recursos
| Recurso | react-dropzone | react-uploady | Uppy | Dropup |
|---|---|---|---|---|
| Arrastar e Soltar | ✓ | ✓ | ✓ | ✓ |
| Upload | ✗ | ✓ | ✓ | ✓ |
| Progresso | ✗ | ✓ | ✓ | ✓ |
| Partes | ✗ | ✓ | ✓ | ✓ |
| tus | ✗ | ✓ | ✓ | ✓ |
| Nuvem (S3) | ✗ | ✓ | ✓ | ✓ |
| React Native | ✗ | ✗ | ✗ | ✓ |
| Tamanho do Bundle | ~10KB | ~30KB | ~100KB+ | <10KB |
| TypeScript | ✓ | ✓ | ✓ | ✓ |
| Headless | ✓ | Parcial | ✗ | ✓ |