Skip to content

Commit 501a5ac

Browse files
committed
$'syncing commit from monorepo. PR: 394, Title: FIO-9944: Fixes a memory leak where create form promise fullfills only after original component got unmount'
1 parent 4e89f0e commit 501a5ac

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

src/components/Form.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ export const Form = (props: FormProps) => {
268268
...handlers
269269
} = props;
270270
const [formInstance, setFormInstance] = useState<Webform | null>(null);
271+
const isMounted = useRef(false);
271272

272273
useEffect(() => {
273274
return () => {
@@ -277,6 +278,13 @@ export const Form = (props: FormProps) => {
277278
};
278279
}, [formInstance]);
279280

281+
useEffect(() => {
282+
isMounted.current = true;
283+
return () => {
284+
isMounted.current = false;
285+
}
286+
}, []);
287+
280288
useEffect(() => {
281289
if (
282290
typeof formSource === 'object' &&
@@ -308,6 +316,10 @@ export const Form = (props: FormProps) => {
308316
);
309317

310318
if (instance) {
319+
if (!isMounted.current) {
320+
instance.destroy(true);
321+
return;
322+
}
311323
if (typeof formSource === 'string') {
312324
instance.src = formSource;
313325
} else if (typeof formSource === 'object') {
@@ -321,7 +333,7 @@ export const Form = (props: FormProps) => {
321333
if (formReadyCallback) {
322334
formReadyCallback(instance);
323335
}
324-
setFormInstance((prevInstance: any) => {
336+
setFormInstance((prevInstance) => {
325337
if (prevInstance) {
326338
prevInstance.destroy(true);
327339
}
@@ -333,14 +345,7 @@ export const Form = (props: FormProps) => {
333345
};
334346

335347
createInstance();
336-
}, [
337-
formConstructor,
338-
formReadyCallback,
339-
formSource,
340-
options,
341-
url,
342-
submission,
343-
]);
348+
}, [formConstructor, formReadyCallback, formSource, options, url]);
344349

345350
useEffect(() => {
346351
let onAnyHandler = null;
@@ -359,7 +364,9 @@ export const Form = (props: FormProps) => {
359364

360365
useEffect(() => {
361366
if (formInstance && submission) {
362-
formInstance.submission = submission;
367+
if (!Utils._.isEqual(formInstance.submission, submission)) {
368+
formInstance.submission = submission;
369+
}
363370
}
364371
}, [formInstance, submission]);
365372

0 commit comments

Comments
 (0)