I’m trying to use file upload with preview and this is the code of my component:
const [uploadField, setUploadFiled] = useState()
useEffect(() => {
const temp = new FileUploadWithPreview('fileUpload', {
multiple: multiple,
});
window.addEventListener(Events.IMAGE_ADDED, (e) => {
const { detail } = e;
console.log('detail', detail);
});
}, [])
The problem is that since I have <React.StrictMode>
I see two file upload controls in my page. And whenever I save the file, because of HMR another control would be created.
I want to only run that initialization code once.
How can I achieve that?
2
Answers
You can use the
useRef
hook to store a reference to theFileUploadWithPreview
instance.This will ensure that the code is only executed once, even when HMR is enabled.
try useRef hook