คู่มือการย้ายข้อมูล
วิธีการย้ายมาใช้ 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/*' |
| ไม่รองรับอัปโหลด | อัปโหลดในตัวพร้อมติดตามความคืบหน้า |
| ล้าง preview ด้วยตนเอง | ล้างอัตโนมัติ |
จาก react-uploady
react-uploady เป็นไลบรารีอัปโหลดที่มีฟีเจอร์มากมายพร้อมหลาย components
ก่อน (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 + Components | Hook เดียว |
useItemProgressListener | callback onUploadProgress |
processPending() | actions.upload() |
| หลาย packages | ครบในที่เดียว |
จาก 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 |
|---|---|
| Component พร้อม UI ในตัว | Headless (ใช้ UI ของคุณเอง) |
getUploadParams | ตัวเลือก upload |
onChangeStatus | Callbacks เฉพาะ |
| ต้อง import CSS | ไม่มี styles |
จาก 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 ครบในตัว | Headless |
| ระบบ Plugin | ครบในที่เดียว |
| Bundle ขนาดใหญ่ | < 10KB |
| Imperative API | React hooks |
ขั้นตอนการย้ายทั่วไป
1. ติดตั้ง Dropup
npm uninstall react-dropzone react-uploady @rpldy/uploady # ลบตัวเก่า
npm install @samithahansaka/dropup
2. อัปเดต Imports
// ก่อน
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. อัปเดต Event Handlers
// ก่อน (react-dropzone)
onDrop: (files) => {...}
// หลัง (Dropup)
onFilesAdded: (files) => {...}
onUploadComplete: (file) => {...}
6. อัปเดต Template
// ก่อน
<div {...getRootProps()}>
<input {...getInputProps()} />
</div>
// หลัง
<div {...getDropProps()}>
<input {...getInputProps()} />
</div>
เปรียบเทียบฟีเจอร์
| ฟีเจอร์ | react-dropzone | react-uploady | Uppy | Dropup |
|---|---|---|---|---|
| ลากและวาง | ✓ | ✓ | ✓ | ✓ |
| อัปโหลด | ✗ | ✓ | ✓ | ✓ |
| ติดตามความคืบหน้า | ✗ | ✓ | ✓ | ✓ |
| แบ่งชิ้น | ✗ | ✓ | ✓ | ✓ |
| tus | ✗ | ✓ | ✓ | ✓ |
| Cloud (S3) | ✗ | ✓ | ✓ | ✓ |
| React Native | ✗ | ✗ | ✗ | ✓ |
| ขนาด Bundle | ~10KB | ~30KB | ~100KB+ | <10KB |
| TypeScript | ✓ | ✓ | ✓ | ✓ |
| Headless | ✓ | บางส่วน | ✗ | ✓ |