问题描述
当我获取数据时:无法对卸载的组件执行 React 状态更新.该应用程序仍然有效,但 React 表明我可能导致内存泄漏.
When fetching data I'm getting: Can't perform a React state update on an unmounted component. The app still works, but react is suggesting I might be causing a memory leak.
这是一个空操作,但它表明您的应用程序中存在内存泄漏.要修复,请取消 useEffect 清理函数中的所有订阅和异步任务."
为什么我总是收到这个警告?
Why do I keep getting this warning?
我尝试研究这些解决方案:
I tried researching these solutions:
https://developer.mozilla.org/en-US/文档/Web/API/AbortSignal
https://developer.mozilla.org/en-US/文档/Web/API/AbortController
但这仍然给我警告.
const ArtistProfile = props => {
const [artistData, setArtistData] = useState(null)
const token = props.spotifyAPI.user_token
const fetchData = () => {
const id = window.location.pathname.split("/").pop()
console.log(id)
props.spotifyAPI.getArtistProfile(id, ["album"], "US", 10)
.then(data => {setArtistData(data)})
}
useEffect(() => {
fetchData()
return () => { props.spotifyAPI.cancelRequest() }
}, [])
return (
<ArtistProfileContainer>
<AlbumContainer>
{artistData ? artistData.artistAlbums.items.map(album => {
return (
<AlbumTag
image={album.images[0].url}
name={album.name}
artists={album.artists}
key={album.id}
/>
)
})
: null}
</AlbumContainer>
</ArtistProfileContainer>
)
}
在我的 api 文件中,我添加了一个 AbortController()
并使用了一个 signal
以便我可以取消请求.
In my api file I added an AbortController()
and used a signal
so I can cancel a request.
export function spotifyAPI() {
const controller = new AbortController()
const signal = controller.signal
// code ...
this.getArtist = (id) => {
return (
fetch(
`https://api.spotify.com/v1/artists/${id}`, {
headers: {"Authorization": "Bearer " + this.user_token}
}, {signal})
.then(response => {
return checkServerStat(response.status, response.json())
})
)
}
// code ...
// this is my cancel method
this.cancelRequest = () => controller.abort()
}
我的 spotify.getArtistProfile()
看起来像这样
this.getArtistProfile = (id,includeGroups,market,limit,offset) => {
return Promise.all([
this.getArtist(id),
this.getArtistAlbums(id,includeGroups,market,limit,offset),
this.getArtistTopTracks(id,market)
])
.then(response => {
return ({
artist: response[0],
artistAlbums: response[1],
artistTopTracks: response[2]
})
})
}
但是因为我的信号用于在 Promise.all
中解析的单个 api 调用,我不能 abort()
那个承诺所以我将始终设置国家.
but because my signal is used for individual api calls that are resolved in a Promise.all
I can't abort()
that promise so I will always be setting the state.
推荐答案
在 fetch()
请求之间共享 AbortController
是正确的方法.
当 任何 Promise
被中止时,Promise.all()
将拒绝 AbortError代码>:
Sharing the AbortController
between the fetch()
requests is the right approach.
When any of the Promise
s are aborted, Promise.all()
will reject with AbortError
:
function Component(props) {
const [fetched, setFetched] = React.useState(false);
React.useEffect(() => {
const ac = new AbortController();
Promise.all([
fetch('http://placekitten.com/1000/1000', {signal: ac.signal}),
fetch('http://placekitten.com/2000/2000', {signal: ac.signal})
]).then(() => setFetched(true))
.catch(ex => console.error(ex));
return () => ac.abort(); // Abort both fetches on unmount
}, []);
return fetched;
}
const main = document.querySelector('main');
ReactDOM.render(React.createElement(Component), main);
setTimeout(() => ReactDOM.unmountComponentAtNode(main), 1); // Unmount after 1ms
<script src="//cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.development.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.development.js"></script>
<main></main>
这篇关于React useEffect 导致:无法对未安装的组件执行 React 状态更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!