迁移指南
如何从其他流行的上传库迁移到 Dropup。
从 react-dropzone 迁移
react-dropzone 是一个流行的拖放库。Dropup 提供类似的功能,并内置上传支持。
之前(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>放在这里</p> : <p>拖放文件到此处</p>}
</div>
);
}
之后(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' }, // 内置上传!
});
return (
<div {...getDropProps()}>
<input {...getInputProps()} />
{state.isDragging ? <p>放在这里</p> : <p>拖放文件到此处</p>}
{files.map(file => (
<div key={file.id}>
{file.preview && <img src={file.preview} />}
<span>{file.progress}%</span>
</div>
))}
<button onClick={() => actions.upload()}>上传</button>
</div>
);
}
主要区别
| react-dropzone | Dropup |
|---|---|
getRootProps() | getDropProps() |
isDragActive | state.isDragging |
accept: { 'image/*': [] } | accept: 'image/*' |
| 无上传支持 | 内置上传和进度追踪 |
| 手动清理预览 | 自动清理 |
从 react-uploady 迁移
react-uploady 是一个功能丰富的上传库,包含多个组件。
之前(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>拖放文件到此处</div>
</UploadDropZone>
<UploadProgress />
</Uploady>
);
}
function UploadProgress() {
useItemProgressListener((item) => {
console.log(`${item.file.name}: ${item.completed}%`);
});
const { processPending } = useUploady();
return <button onClick={processPending}>上传</button>;
}
之后(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>拖放文件到此处</div>
{files.map(file => (
<div key={file.id}>
{file.name}: {file.progress}%
</div>
))}
<button onClick={() => actions.upload()}>上传</button>
</div>
);
}
主要区别
| react-uploady | Dropup |
|---|---|
| Provider + 组件 | 单一 hook |
useItemProgressListener | onUploadProgress 回调 |
processPending() | actions.upload() |
| 多个包 | 一体化 |
从 react-dropzone-uploader 迁移
react-dropzone-uploader 将 dropzone 与上传功能结合在一起。
之前(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}
/>
);
}
之后(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('上传中', file),
onUploadComplete: (file) => console.log('完成', file),
onUploadError: (file, err) => console.log('错误', file, err),
});
return (
<div {...getDropProps()}>
<input {...getInputProps()} />
{/* 您的自定义 UI */}
</div>
);
}
主要区别
| react-dropzone-uploader | Dropup |
|---|---|
| 带内置 UI 的组件 | 无头(自己构建 UI) |
getUploadParams | upload 选项 |
onChangeStatus | 特定回调 |
| 需要导入 CSS | 无样式 |
从 Uppy 迁移
Uppy 是一个功能完整的上传工具包。
之前(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('成功:', file, response);
});
function OldUploader() {
return <div id="uppy" />;
}
之后(Dropup)
import { useDropup } from '@samithahansaka/dropup';
function NewUploader() {
const { files, actions, state, getDropProps, getInputProps } = useDropup({
upload: { url: '/api/upload' },
onUploadComplete: (file, response) => {
console.log('成功:', file, response);
},
});
return (
<div>
<div {...getDropProps()}>
<input {...getInputProps()} />
拖放文件到此处
</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()}>上传</button>
</div>
);
}
主要区别
| Uppy | Dropup |
|---|---|
| 包含完整 UI | 无头 |
| 插件系统 | 一体化 |
| 较大的包体积 | < 10KB |
| 命令式 API | React hooks |
通用迁移步骤
1. 安装 Dropup
npm uninstall react-dropzone react-uploady @rpldy/uploady # 移除旧的
npm install @samithahansaka/dropup
2. 更新导入
// 之前
import { useDropzone } from 'react-dropzone';
// 之后
import { useDropup } from '@samithahansaka/dropup';
3. 更新 Hook 用法
// 之前
const { getRootProps, getInputProps, isDragActive } = useDropzone({...});
// 之后
const { getDropProps, getInputProps, state } = useDropup({...});
// 使用 state.isDragging 替代 isDragActive
4. 添加上传配置
// Dropup 包含上传功能
useDropup({
upload: { url: '/api/upload' },
});
5. 更新事件处理器
// 之前(react-dropzone)
onDrop: (files) => {...}
// 之后(Dropup)
onFilesAdded: (files) => {...}
onUploadComplete: (file) => {...}
6. 更新模板
// 之前
<div {...getRootProps()}>
<input {...getInputProps()} />
</div>
// 之后
<div {...getDropProps()}>
<input {...getInputProps()} />
</div>
功能比较
| 功能 | react-dropzone | react-uploady | Uppy | Dropup |
|---|---|---|---|---|
| 拖放 | ✓ | ✓ | ✓ | ✓ |
| 上传 | ✗ | ✓ | ✓ | ✓ |
| 进度 | ✗ | ✓ | ✓ | ✓ |
| 分块 | ✗ | ✓ | ✓ | ✓ |
| tus | ✗ | ✓ | ✓ | ✓ |
| 云存储 (S3) | ✗ | ✓ | ✓ | ✓ |
| React Native | ✗ | ✗ | ✗ | ✓ |
| 包大小 | ~10KB | ~30KB | ~100KB+ | <10KB |
| TypeScript | ✓ | ✓ | ✓ | ✓ |
| 无头 | ✓ | 部分 | ✗ | ✓ |