UseEffect无法执行状态更新
我收到此 UseEffct 错误,导致我的窗口呈现空页面 error ="无法对已卸载的组件执行 React 状态更新。这是一个空操作,但它表明应用程序中存在内存泄漏。要修复此问题,请取消 useEffect 清理函数中的所有订阅和异步任务。"
我的组件代码
import React, { useEffect, useState } from "react";
import { instance } from "../Data/axios";
const Row = ({ title, fetchURL }) => {
const [movies, setMovies] = useState([]);
// A snippet of code that runs on a certain condition
useEffect(() => {
async function fetchData() {
const data = await instance.get(fetchURL);
setMovies(data.data.results);
}
fetchData();
}, [fetchURL]);
const base_url = "https://image.tmdb.org/t/p/original/";
console.log(movies);
return (
<div className="rows">
<h3>{title}</h3>
<div className="rows_poster">
{movies.map((movie) => {
<div key={movie.id}>
<img src={`${base_url}${movie.poster_path}`} />
</div>;
})}
</div>
</div>
);
};
export default Row;
I am getting this UseEffct error that causes my window to render an empty page
error ="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."
my Component Code
import React, { useEffect, useState } from "react";
import { instance } from "../Data/axios";
const Row = ({ title, fetchURL }) => {
const [movies, setMovies] = useState([]);
// A snippet of code that runs on a certain condition
useEffect(() => {
async function fetchData() {
const data = await instance.get(fetchURL);
setMovies(data.data.results);
}
fetchData();
}, [fetchURL]);
const base_url = "https://image.tmdb.org/t/p/original/";
console.log(movies);
return (
<div className="rows">
<h3>{title}</h3>
<div className="rows_poster">
{movies.map((movie) => {
<div key={movie.id}>
<img src={`${base_url}${movie.poster_path}`} />
</div>;
})}
</div>
</div>
);
};
export default Row;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我有用:
This has worked for me: