ප්‍රධාන අන්තර්ගතයට පනින්න

Migration මාර්ගෝපදේශය

අනෙකුත් ජනප්‍රිය upload libraries වලින් Dropup වෙත migrate කරන ආකාරය.

react-dropzone වෙතින්

react-dropzone යනු ජනප්‍රිය drag-and-drop library එකකි. Dropup built-in upload support සමඟ සමාන functionality සපයයි.

පෙර (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>ගොනු මෙතැනට drag කරන්න</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' }, // Built-in upload!
});

return (
<div {...getDropProps()}>
<input {...getInputProps()} />
{state.isDragging ? <p>මෙතැනට දමන්න</p> : <p>ගොනු මෙතැනට drag කරන්න</p>}

{files.map(file => (
<div key={file.id}>
{file.preview && <img src={file.preview} />}
<span>{file.progress}%</span>
</div>
))}

<button onClick={() => actions.upload()}>Upload</button>
</div>
);
}

ප්‍රධාන වෙනස්කම්

react-dropzoneDropup
getRootProps()getDropProps()
isDragActivestate.isDragging
accept: { 'image/*': [] }accept: 'image/*'
Upload support නැතProgress සමඟ built-in upload
Manual preview cleanupස්වයංක්‍රීය cleanup

react-uploady වෙතින්

react-uploady යනු components කිහිපයක් සහිත feature-rich upload library එකකි.

පෙර (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}>Upload</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()}>Upload</button>
</div>
);
}

ප්‍රධාන වෙනස්කම්

react-uploadyDropup
Provider + Componentsඑක hook එකක්
useItemProgressListeneronUploadProgress callback
processPending()actions.upload()
බහු packagesAll-in-one

react-dropzone-uploader වෙතින්

react-dropzone-uploader dropzone එක upload functionality සමඟ ඒකාබද්ධ කරයි.

පෙර (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('uploading', file),
onUploadComplete: (file) => console.log('done', file),
onUploadError: (file, err) => console.log('error', file, err),
});

return (
<div {...getDropProps()}>
<input {...getInputProps()} />
{/* ඔබගේ custom UI */}
</div>
);
}

ප්‍රධාන වෙනස්කම්

react-dropzone-uploaderDropup
Built-in UI සහිත ComponentHeadless (ඔබගේම UI ගෙනෙන්න)
getUploadParamsupload option
onChangeStatusනිශ්චිත callbacks
CSS import අවශ්‍යයිStyles නැත

Uppy වෙතින්

Uppy යනු full-featured upload toolkit එකකි.

පෙර (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()}>Upload</button>
</div>
);
}

ප්‍රධාන වෙනස්කම්

UppyDropup
සම්පූර්ණ UI ඇතුළත්Headless
Plugin systemAll-in-one
විශාල bundle< 10KB
Imperative APIReact hooks

පොදු Migration පියවර

1. Dropup Install කරන්න

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({...});
// isDragActive වෙනුවට state.isDragging භාවිතා කරන්න

4. Upload Config එකතු කරන්න

// Dropup upload functionality ඇතුළත්
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>

Feature සංසන්දනය

Featurereact-dropzonereact-uploadyUppyDropup
Drag & Drop
Upload
Progress
Chunked
tus
Cloud (S3)
React Native
Bundle Size~10KB~30KB~100KB+<10KB
TypeScript
Headlessඅර්ධ වශයෙන්