Skip to content

Commit c6451d4

Browse files
committed
fix: fix
1 parent 8cbe4c2 commit c6451d4

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

src/AjaxUploader.tsx

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,27 @@ const AjaxUploader: React.FC<Readonly<React.PropsWithChildren<UploadProps>>> = p
4747
} = props;
4848

4949
const [uid, setUid] = React.useState<string>(getUid);
50-
const [reqs, setReqs] = React.useState<Record<PropertyKey, any>>({});
5150

5251
const isMountedRef = React.useRef<boolean>(false);
5352
const inputRef = React.useRef<HTMLInputElement>(null);
53+
const reqsRef = React.useRef<Partial<Record<PropertyKey, any>>>({});
5454

55-
const abort = React.useCallback(
56-
(file?: any) => {
57-
if (file) {
58-
const internalUid = file.uid ? file.uid : file;
59-
if (reqs[internalUid]?.abort) {
60-
reqs[internalUid].abort();
61-
}
62-
setReqs(prev => {
63-
const { [internalUid]: _, ...rest } = prev;
64-
return rest;
65-
});
66-
} else {
67-
Object.keys(reqs).forEach(key => {
68-
if (reqs[key]?.abort) {
69-
reqs[key].abort();
70-
}
71-
});
72-
setReqs({});
55+
const abort = React.useCallback((file?: any) => {
56+
if (file) {
57+
const internalUid = file.uid ? file.uid : file;
58+
if (reqsRef.current[internalUid]?.abort) {
59+
reqsRef.current[internalUid].abort();
7360
}
74-
},
75-
[reqs],
76-
);
61+
reqsRef.current[internalUid] = undefined;
62+
} else {
63+
Object.keys(reqsRef.current).forEach(key => {
64+
if (reqsRef.current[key]?.abort) {
65+
reqsRef.current[key].abort();
66+
}
67+
});
68+
reqsRef.current = {};
69+
}
70+
}, []);
7771

7872
React.useEffect(() => {
7973
isMountedRef.current = true;
@@ -172,21 +166,15 @@ const AjaxUploader: React.FC<Readonly<React.PropsWithChildren<UploadProps>>> = p
172166
},
173167
onSuccess: (ret: any, xhr: XMLHttpRequest) => {
174168
props.onSuccess?.(ret, parsedFile, xhr);
175-
setReqs(prev => {
176-
const { [origin.uid]: _, ...rest } = prev;
177-
return rest;
178-
});
169+
reqsRef.current[origin.uid] = undefined;
179170
},
180171
onError: (err: UploadRequestError, ret: any) => {
181172
props.onError?.(err, ret, parsedFile);
182-
setReqs(prev => {
183-
const { [origin.uid]: _, ...rest } = prev;
184-
return rest;
185-
});
173+
reqsRef.current[origin.uid] = undefined;
186174
},
187175
};
188176
onStart(origin);
189-
setReqs(prev => ({ ...prev, [origin.uid]: request(requestOption) }));
177+
reqsRef.current[origin.uid] = request(requestOption);
190178
};
191179

192180
const uploadFiles = (files: File[]) => {

0 commit comments

Comments
 (0)