简体   繁体   English

React state 未定义,即使 console.log 显示它

[英]React state is undefined , even though console.log shows it

I have researched other cases when the state was unidentified but I am still unsuccessfully trying to subtract data from UI actions that is in the format [{},{}...].我研究了 state 无法识别的其他情况,但我仍然未能成功尝试从格式为 [{},{}...] 的 UI 操作中减去数据。 I have managed to add to the array, using this code, which in the same time also computes the total for the item (subTotal and products are props from a child component got through a callback function):我已经设法使用此代码添加到数组中,同时它还计算项目的总数(subTotal 和 products 是通过回调函数获得的子组件的道具):

const updateTotalPriceAndUpdatePieChartData = (subTotal,product) => {
        //from here   
        setPieChartData([...pieChartData,{product,subTotal}])
        //until up, we handle what data we need for the pieChart
        setTotal(total => total + (Number.isFinite(subTotal) ? subTotal : 0))
        console.log("TOTAL WAS COMPUTED")
    }

And this is the state that holds the array:这是保存数组的 state:

const [pieChartData,setPieChartData]=React.useState([])

Yet when I try to delte an object from the state array (when an item is also deleted), I try the following:然而,当我尝试从 state 数组中删除一个 object 时(当一个项目也被删除时),我尝试以下操作:

const substractSubTotalAndSubstractTotalForPieChart = (subTotal,product,pieChartData) => {
    setTotal(total - subTotal)
    const lastPieChartData=pieChartData.filter(item => item !={subTotal,product})
    setPieChartData(lastPieChartData)
}

It says that pieChartData is unidentified.它说pieChartData身份不明。 Could you please let me know what I can do?你能告诉我我能做什么吗?

In substractSubTotalAndSubstractTotalForPieChart method, there is a paramter pieChartData , but there is also a state with the same name .substractSubTotalAndSubstractTotalForPieChart方法中,有一个参数pieChartData ,但也有一个同名state So what happens is inside the function substractSubTotalAndSubstractTotalForPieChart's scope, the parameter pieChartData is given preference rather than the state.所以在 function substractSubTotalAndSubstractTotalForPieChart substractSubTotalAndSubstractTotalForPieChart's scope 内部会发生什么,参数pieChartData优先于 Z9ED39E2EA931586B76A985A69E24。

Hence in your method invocation, when you don't pass this parameter, the default value of an uninitialized parameter, ie, undefined is used.因此,在您的方法调用中,当您不传递此参数时,将使用未初始化参数的默认值,即 undefined。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JS 说 object 是未定义的,即使它显示在 console.log 中 - JS says an object is undefined even though it shows with console.log Console.log 只显示原始 state,尽管我 100% 确定 state 正在 React 中更新 - Console.log only ever shows the original state, even though I'm 100% sure the state is being updated in React 反应 - 更新 state 然后执行 console.log,显示未更新的 state - REACT - Updating state and then doing a console.log , shows unupdated state 变量是未定义的错误(即使console.log显示变量) - Variable is undefined error (even if console.log shows variable) 即使console.log清楚地显示了具有所需属性的对象,也无法访问对象属性(React Redux) - Can't access object property even though console.log clearly shows an Object with the needed property (React Redux) 即使在控制台日志上定义了 React state 也未定义 - React state is undefined even if on console log is defined 即使之前的 console.log 显示该语句应该运行,if 语句也不会运行 - if statement doen't run even though the console.log before that shows the statement should run javascript函数即使在console.log中显示值也返回未定义 - javascript function returning undefined even though value display in console.log req.user 未定义,即使它能够 console.log 用户 - req.user is undefined even though it is able to console.log the user 即使对象上的console.log显示属性值,对象的属性也未定义 - Property of object is undefined, even though console.log on object is showing the property value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM