Skip to content

Instantly share code, notes, and snippets.

@jsmanifest
Created July 3, 2019 16:11
Show Gist options
  • Select an option

  • Save jsmanifest/ec950caf1f3f1120711d510f13a189d0 to your computer and use it in GitHub Desktop.

Select an option

Save jsmanifest/ec950caf1f3f1120711d510f13a189d0 to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from 'react'
const useUploadStuff = () => {
const [files, setFiles] = useState([])
// Limit the file sizes here
const onChange = (e) => {
const arrFiles = Array.from(e.target.files)
const filesUnder5mb = arrFiles.filter((file) => {
const bytesLimit = 5000000
if (file.size > bytesLimit) {
// optionally process some UX about this file size
}
return file.size < bytesLimit
})
setFiles(filesUnder5mb)
}
useEffect(() => {
if (files.length) {
// do something with files
}
}, [files])
return {
files,
onChange,
}
}
const UploadStuff = () => {
const { onChange } = useUploadStuff()
return (
<div>
<h2 style={{ color: '#fff' }}>Hi</h2>
<div>
<input
style={{ color: '#fff' }}
onChange={onChange}
type="file"
placeholder="Upload Stuff"
multiple
/>
</div>
</div>
)
}
export default UploadStuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment