Created
July 3, 2019 16:11
-
-
Save jsmanifest/ec950caf1f3f1120711d510f13a189d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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