遷移指南
如何從其他流行的上傳函式庫遷移到 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 | ✓ | ✓ | ✓ | ✓ |
| 無樣式 | ✓ | 部分 | ✗ | ✓ |