我有一个非常奇怪的问题,控制台返回警告:无法对未安装的组件执行React状态更新。这是一个no-op,但它表示应用程序中存在内存泄漏。要修复此问题,请取消useEffect清理函数中的所有订阅和异步任务。
我阅读了这里的主题,并尝试了许多解决方案,包括未安装的示例true和false,但我没有解决我的问题代码如下:
const [getCompany, setgetCompany] = useState('');
useEffect(() => {
const fetchCompany = async () => {
try {
const responseData = await sendRequest(
`http://localhost:5000/api/companies/${props.company}`,
);
setgetCompany(responseData);
} catch (err) {}
};
props.company && fetchCompany();
}, [props.company]);
作为回报,我这样做了:
{(() => {
if(getCompany){
return Object.keys(getCompany).map((item,index) => {
return(
<img
key={getCompany[item].id}
src={`http://localhost:5000/${getCompany[item].image}`}
alt={getCompany[item].title}
/>
)
})
}
})()}
这只是功能,从公司获得详细信息,如照片,任何帮助?
你需要取消你的promise
import React, { useEffect, useState } from "react";
import { CPromise, CanceledError } from "c-promise2";
import cpFetch from "cp-fetch";
function MyComponent(props) {
const [text, setText] = useState("fetching...");
useEffect(() => {
console.log("mount");
const promise = CPromise.from(function* () {
try {
const response = yield cpFetch(props.url);
const json = yield response.json();
setText(`Success: ${JSON.stringify(json)}`);
} catch (err) {
console.warn(err);
CanceledError.rethrow(err); //passthrough
// handle other errors than CanceledError
setText(`Failed: ${err}`);
}
}, []);
return () => {
console.log("unmount");
promise.cancel();
};
}, [props.url]);
return <p>{text}</p>;
}