我得到这个警告,当我使用我的提取后方法如何我可以取消所有订阅和异步任务中的使用效果清理功能。用我的文章方法。
警告:无法对已卸载的组件执行React状态更新。这是一个no-op,但它表示应用程序中存在内存泄漏。要修复此问题,请取消useEffect清理函数中的所有订阅和异步任务。
import React from "react";
import { useHistory } from "react-router-dom";
import { UPLOAD_PRESET, CLOUD_NAME, SERVER_API } from "../../config";
const uploadImage = async (file) => {
const url = `https://api.cloudinary.com/v1_1/${CLOUD_NAME}/upload`;
const formData = new FormData();
formData.append("file", file);
formData.append("upload_preset", UPLOAD_PRESET);
const res = await fetch(url, {
method: "POST",
body: formData,
});
if (!res.ok) {
throw new Error(`Can't upload image. ${res.status}`);
}
const data = await res.json();
return await data.eager[0].secure_url;
};
const createAlbum = async (data) => {
const res = await fetch(`${SERVER_API}/api/v1/albums`, {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
});
if (!res.ok) {
throw new Error(`An error has occurred: ${res.status}`);
}
const json = await res.json();
return json.data._id;
};
const Form = ({ file, loading, setError, album, color, children }) => {
let history = useHistory();
const clearError = () => setError("");
const handleSubmit = async (e) => {
e.preventDefault();
clearError();
try {
if (!file) {
throw new Error("Please select a file to add.");
}
if (!album.trim("") || !color.trim()) {
throw new Error("Please enter all the field values.");
}
loading(true);
const fileUrl = await uploadImage(file);
const data = {
name: album,
bckImgUrl: fileUrl,
color: color,
};
const albumId = await createAlbum(data);
history.push(`/albums/${albumId}`);
} catch (error) {
setError(error.message);
} finally {
loading(false);
}
};
return <form onSubmit={handleSubmit}>{children}</form>;
};
export default Form;
我同意Ramesh使用ref。我想我会展示如何将它提取到自定义挂钩中。
function useHasUnmountedRef() {
const hasUnmountedRef = useRef(false);
useEffect(() => {
return () => {
hasUnmountedRef.current = true;
}
}, []);
return hasUnmountedRef;
}
function Form() {
const hasUnmountedRef = useHasUnmountedRef();
const handleSubmit = async () => {
await asyncStuff();
if (hasUnmountedRef.current) {
// escape early because component has unmounted
return;
}
// Thanks to the guard clause above, you can guarantee at this
// point that your component is still mounted. You can perform
// state updates without generating a React warning.
//
// If you do another "await" however, you will need to check
// again. Everytime you await something async, there is a chance
// that the component could have unmounted while waiting for the
// async stuff to complete.
};
return (
<form onSubmit={handleSubmit} />
);
}
export default Form;
您可以使用React.useRef
:
在您的组件中:
const hasUnmounted = React.useRef(false);
React.useEffect(() => {
// maybe you have your side effects here, so you can use the boolean to avoid
// state updates when the component is unmounted
if(!hasUnmounted.current) {
// do state update
}
return () => {
hasUnmounted.current = true;
}
}, [])
React.useRef
适用于解决这个问题,因为它与更改组件的状态非常不同,这有点像类组件中的实例变量,更改它不会触发重新渲染。