Skip to content

Commit 5daa202

Browse files
committed
refactored Form component code to prevent the exhaustive-deps error for useEffect
1 parent 76db3fb commit 5daa202

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/components/Form.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,14 @@ export const Form = (props: FormProps) => {
270270
const [formInstance, setFormInstance] = useState<Webform | null>(null);
271271

272272
useEffect(() => {
273-
let ignore = false;
273+
return () => {
274+
if (formInstance) {
275+
formInstance.destroy(true);
276+
}
277+
};
278+
}, [formInstance]);
279+
280+
useEffect(() => {
274281
const createInstance = async () => {
275282
if (renderElement.current === null) {
276283
console.warn('Form element not found');
@@ -290,10 +297,6 @@ export const Form = (props: FormProps) => {
290297
);
291298

292299
if (instance) {
293-
if (ignore) {
294-
instance.destroy(true);
295-
return;
296-
}
297300
if (typeof formSource === 'string') {
298301
instance.src = formSource;
299302
} else if (typeof formSource === 'object') {
@@ -314,13 +317,6 @@ export const Form = (props: FormProps) => {
314317
};
315318

316319
createInstance();
317-
return () => {
318-
ignore = true;
319-
if (formInstance) {
320-
formInstance.destroy(true);
321-
}
322-
};
323-
// eslint-disable-next-line react-hooks/exhaustive-deps
324320
}, [
325321
formConstructor,
326322
formReadyCallback,

0 commit comments

Comments
 (0)