为大家分享一个警告信息
devScripts.js:5836 Warning: Can’t perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
我的代码:
const [gData, setGData] = useState([]);
useEffect(() => {
const fetchData = async () => {
setGData(res.data)
};
fetchData();
}, []);
报错原因:因为我在useEffect里面使用usestart来接收异步请求的值导致
解决方法:
const [gData, setGData] = useState([]);
useEffect(() => {
const fetchData = async () => {
const res = await getUnexploredDirectory()
.then(() => {
setGData(res.data)
}).catch(err => {
return;
})
};
fetchData();
}, []);