[英]ReactJS - warning message 'Hook useEffect has a missing dependency'
I'm getting the message in my console:我在控制台中收到消息:
React Hook useEffect has a missing dependency: 'calculateTotalProductsPrice'.
React Hook useEffect 缺少依赖项:'calculateTotalProductsPrice'。 Either include it or remove the dependency array
包括它或删除依赖项数组
The code is working but I dont know if is this is the proper way of implementing useEffect代码正在运行,但我不知道这是否是实现 useEffect 的正确方法
Here I have a function that calculates the total price of my products and im calling it in my useEffect hook:在这里,我有一个计算产品总价的函数,并在我的 useEffect 钩子中调用它:
// Get all products the user added from my redux store
const allProductsAddedByUser = useSelector(state => state.createCampaign.campaign_products)
function calculateTotalProductsPrice() {
const productsArray = Object.values(allProductsAddedByUser)
const totalPrice = productsArray.reduce(function (prev, cur) {
return prev + cur.quantity * 125.0
}, 0)
return totalPrice
}
useEffect(() => {
calculateTotalProductsPrice()
}, [allProductsAddedByUser])
And in my html, I'm calling the function like this:在我的 html 中,我像这样调用函数:
<span>€ {calculateTotalProductsPrice()}</span>
If I put the calculateTotalProductsPrice()
function inside my useEffect hook, the warning from console is gone, but then I can't call my function like I was doing, I'm getting the error 'calculateTotalProductsPrice' is not defined
.如果我将
calculateTotalProductsPrice()
函数放在我的 useEffect 钩子中,来自控制台的警告消失了,但是我不能像以前那样调用我的函数,我收到错误'calculateTotalProductsPrice' is not defined
。 My function is now inside the useEffect hook, why cant I call her from outside?我的函数现在在 useEffect 钩子里面,为什么我不能从外面调用她?
// Get all products the user added from my redux store
const allProductsAddedByUser = useSelector(state => state.createCampaign.campaign_products)
useEffect(() => {
function calculateTotalProductsPrice() {
const productsArray = Object.values(allProductsAddedByUser)
const totalPrice = productsArray.reduce(function (prev, cur) {
return prev + cur.quantity * 125.0
}, 0)
return totalPrice
}
calculateTotalProductsPrice()
}, [allProductsAddedByUser])
// Call function from my html
<span>€ {calculateTotalProductsPrice()}</span>
// Error calculateTotalProductsPrice is not defined
About the warning message:关于警告信息:
There are 2 scenarios in this case.在这种情况下有 2 种情况。
The first is which is the current scenario when you need the calculateTotalProductsPrice
function somewhere else in your code then you need to create outside of useEffect
.第一个是当前场景,当您需要在代码中的其他地方使用
calculateTotalProductsPrice
函数时,您需要在useEffect
之外创建。 One addition if that's the case which is passing it to so called dependency array, just like the following:如果是这种情况,则将其传递给所谓的依赖数组,就像下面这样:
function calculateTotalProductsPrice() {
// ... your calculation code
}
useEffect(() => {
calculateTotalProductsPrice()
}, [calculateTotalProductsPrice])
In this case the warning message won't be shown again and in the same time it will be accessible in JSX as well, just like <span>€ {calculateTotalProductsPrice()}</span>
.在这种情况下,警告消息不会再次显示,同时它也可以在 JSX 中访问,就像
<span>€ {calculateTotalProductsPrice()}</span>
。
Second is once you don't need the calculateTotalProductsPrice
function outside of useEffect
then you can create inside of the callback as you did in your second example.其次是一旦您不需要
useEffect
之外的calculateTotalProductsPrice
函数,那么您可以像在第二个示例中那样在回调内部创建。
About the accessibility of the function:关于功能的可访问性:
And the reason why the function calculateTotalProductsPrice
is not accessible outside once you declare in useEffect
is becasue of JavaScript scope.一旦在
useEffect
声明,则无法在外部访问函数calculateTotalProductsPrice
的原因是 JavaScript 范围。 The function only accessible inside of useEffect
hook because of closure, from the documentation:由于关闭,该函数只能在
useEffect
钩子内部访问,来自文档:
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).
闭包是捆绑在一起(封闭)的函数与其周围状态(词法环境)的引用的组合。 In other words, a closure gives you access to an outer function's scope from an inner function.
换句话说,闭包使您可以从内部函数访问外部函数的作用域。 In JavaScript, closures are created every time a function is created, at function creation time.
在 JavaScript 中,每次创建函数时都会在创建函数时创建闭包。
Read further here:在此处进一步阅读:
I hope this helps!我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.