use-file
React hook for Filereader
It exports {src,reader}, using reader to read file & get src by hooks FileReader loadend
event.
Install
$ npm install use-file
How to use?
;const demo = { const result reader = ; // const { result, reader } = useFile('defaultPath'); const onChange = { const files = ecurrentTargetfiles; fileslength > 0 && reader; }; return <div> <input onChange=onChange type="file" id="upload-file" placeholder="Upload a Picture" /> result && <img src=result> </div> ;};
What I Do?
reader <- BlackBox {
- attach loadend
- after loadend update result from reader's result
- clean up if unmounted
} -> result
useEventTarget
Why This has a dependency of This is a hook infrastructure for easily package a event hooks.
See more useEventTarget.