reactjs - React Hook React.useEffect 错误
问题描述
React Hook React.useEffect 缺少依赖项:'params.id'。要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps。我项目中的很多地方都出现了这个错误。
import React, { Fragment } from "react";
import ArticleDetails from "./List";
import { connect } from "react-redux";
import {
articleFetchById,
articleFetch,
articleFetchByChannel
} from "../../../redux/action/articles";
const Article = ({
articles,
articleFetchById,
match: { params },
auth,
articleFetch,
articleFetchByChannel
}) => {
React.useEffect(() => {
articleFetchById(params.id);
}, [articleFetchById]);
React.useEffect(() => {
articleFetch();
}, [articleFetch]);
React.useEffect(() => {
articleFetchByChannel();
}, [articleFetchByChannel]);
return (
<Fragment>
<ArticleDetails articles={articles} auth={auth} matchId={params.id} />
</Fragment>
);
};
const mapStateToProps = state => {
return {
articles: state.articles,
auth: state.auth
};
};
export default connect(mapStateToProps, {
articleFetchById,
articleFetch,
articleFetchByChannel
})(Article);
解决方案
您需要params.id
在 useEffect 的第二个参数数组中传递(因为您在 useEffect 中使用它)。那应该可以解决警告。
React.useEffect(() => {
articleFetchById(params.id);
}, [articleFetchById, params.id]);
推荐阅读
- javascript - JavaScript 中哪个对象没有“hasOwnProperty”?
- html - Outlook 中的 Thymeleaf 特殊字符转换
- c - 在 C 中的 fprintf 上保存多个具有不同名称的文件
- linux - 为什么 bash 不能运行 .out 文件?
- arrays - 在 swift 4 中从嵌套的 JSON 字符串构建数组
- jquery - 如何解决此 Jquery 页面刷新错误
- r - onehot 编码:保留列结构
- python - Python tkinter:在列表框中添加垂直和水平滚动条
- c++ - C++ 数组函数
- javascript - 如何在Javascript中将数组映射到对象?