简体   繁体   English

在 React js 中使用 useState 更新数据

[英]update data with useState in React js

I am new to react.我是新来的。 I have faced one issue and not able to solve it.我遇到了一个问题,但无法解决。 I am looking for your help.我正在寻找你的帮助。 I have an array which I have listed below.我有一个数组,我在下面列出了它。 All data are looped and displayed in the view.所有数据都循环显示在视图中。 From my current array, I want to update the count of dietry array[] which is inside the fruits array.从我当前的数组中,我想更新 fruits 数组中的 dietry array[] 的计数。

This is my useState这是我的使用状态

const [foods, setFood] = useState(fruits)

if I console.log(foods) it gives data as below.如果我使用 console.log(foods) 它会提供如下数据。

fruits: [
    {
        id: 1,
        name: 'Banana',
        family: 'abc',
        price: 2.99,
        isEnabled: true,
        dietary: [
            {
                id:1,
                disabled: false,
                group: null,
                selected: false,
                text: 'N/A',
                value: '858090000',
                count:0
            },
            {  
                id:2,
                disabled: true,
                group: null,
                selected: true,
                text: 'N/A',
                value: '80000',
                count:0
            },
        }

This data are looped in a view page and there is onClick handleIncrement method which should increment the count of dietary array of index 0 or index1 etc whichever index sent from handleIncremnt() method.此数据在视图页面中循环,并且有 onClick handleIncrement 方法,该方法应增加索引 0 或 index1 等膳食数组的计数,无论从 handleIncremnt() 方法发送哪个索引。 This is my method这是我的方法

const handleIncrementCount = (dietary_index) => {
    setFood(foods =>
        foods.map((food,index) =>
         dietary_index === food[index] ? {...food, qty:food.count+1}:food
        )
    );
}

I am not able to increase the count, in my view page counts are 0 even if i click the increment button.It shows some error within map我无法增加计数,在我的视图页面计数为 0,即使我单击增量按钮。它显示 map 内的一些错误

Any help is highly appreciated非常感谢任何帮助

I ma looking for a solutions我正在寻找解决方案

There are a few issues with your handleIncrementCount function. Firstly, you are trying to use the dietary_id parameter to find the correct food object to update, but you are using it to access the index property of the food object instead.您的handleIncrementCount function 存在一些问题。首先,您尝试使用dietary_id参数查找要更新的正确食物 object,但您使用它来访问食物 object 的索引属性。 This will always return undefined, and so the function will not be able to find the correct object to update.这将始终返回未定义,因此 function 将无法找到正确的 object 进行更新。

Secondly, you are trying to update the qty property of the food object, but this property does not exist in the food object. Instead, you need to update the count property of the correct dietary object inside the food object.其次,您正在尝试更新食物 object 的数量属性,但该属性在食物 object 中不存在。相反,您需要更新食物 object 中正确饮食 object 的计数属性。

Here is how you can fix these issues:以下是解决这些问题的方法:

const handleIncrementCount = (dietary_id) => {
    setFood(foods =>
        foods.map(food => {
            // Find the dietary object with the correct id
            const dietary = food.dietary.find(d => d.id === dietary_id);

            // If the dietary object was found, increment its count
            if (dietary) {
                dietary.count += 1;
            }

            // Return the updated food object
            return food;
        })
    );
};

With this change, the handleIncrementCount function should be able to find the correct food object and update the count property of the correct dietary object.通过此更改, handleIncrementCount function 应该能够找到正确的食物 object 并更新正确饮食 object 的计数属性。

Note that this function is using the Array.prototype.map() and Array.prototype.find() methods to transform the foods array and find the correct dietary object to update.请注意,这个 function 正在使用Array.prototype.map()Array.prototype.find()方法来转换食物数组并找到正确的饮食 object 进行更新。 These methods are commonly used in JavaScript to transform and find elements in arrays. It is worth reading up on these methods and understanding how they work in order to better understand this code.这些方法在 JavaScript 中常用来转换和查找 arrays 中的元素。值得阅读这些方法并了解它们的工作原理,以便更好地理解这段代码。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM