Skip to content

Commit 5d5d4aa

Browse files
committed
Remount dropzone when options change
1 parent 32ed32e commit 5d5d4aa

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/UploadDropzone.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Uploader, UploaderResult, UploaderOptions } from "uploader";
2-
import React, { useLayoutEffect } from "react";
2+
import React, { useLayoutEffect, useState } from "react";
33
import { useElementRef } from "react-uploader/Utils";
44

55
interface Props {
@@ -11,7 +11,7 @@ interface Props {
1111
width?: string;
1212
}
1313

14-
export const UploadDropzone = ({ uploader, options, onComplete, onUpdate, width, height }: Props): JSX.Element => {
14+
const UploadDropzoneInner = ({ uploader, options, onComplete, onUpdate, width, height }: Props): JSX.Element => {
1515
const [element, elementRef] = useElementRef();
1616

1717
useLayoutEffect(() => {
@@ -43,3 +43,11 @@ export const UploadDropzone = ({ uploader, options, onComplete, onUpdate, width,
4343
/>
4444
);
4545
};
46+
47+
// We use 'JSON.stringify(props.options)' to force a reinitialization when options like colour and tags change.
48+
// Unfortunately this won't trigger a re-render in the event of a callback changing.
49+
// JSON.stringify will simply elide non-serializable fields like functions.
50+
export const UploadDropzone = (props: Props): JSX.Element => {
51+
const [id] = useState(Math.random().toString());
52+
return <UploadDropzoneInner {...props} key={`${id}-${JSON.stringify(props.options)}`} />;
53+
};

0 commit comments

Comments
 (0)