React JS 将动态对象值从 FORM 添加到对象

时间:2021-03-28 17:05:04

标签: javascript node.js reactjs express ecmascript-6

我有一个对象数组,我需要一个键值

状态:

#include <cs50.h>
#include <stdio.h>

int get_positive_int(void);

int main(void)
{
    int n = get_positive_int();
    
    for(int i = 0; i < n; i++)
    {
        for(int j=0; j < n*2 + 2; j++)
        {
            if ( j < (n-1-i) || j == n || j == n+1 || j > n+2+i) printf(" ");
            else printf("#");
        }
        printf("\n");
    }
}

表单输入更改功能

const [row, setRow] = useState([{ nameofthework: "", schedulerefNo: "", unitprice: "", qty: "", uom: "", gst: "", total: "" }]);

我的表格

const handleInputChange = (e, index) => {
    const { name, value } = e.target;
    const list = [...row];
    list[index][name] = value;
    setRow(list);
    console.log(row);  // Prints out the each row object into and array of objects
};

const handleQty = (e) => {
    const scheduleNumberForQuantity = e.target.value;
    projectScheduleNumber?.filter((v) => {
        if(v["Schedule No"] === scheduleNumberForQuantity ) {
            setScheduleQuantity(v["LOA Qty"]); // return only integer example 1000
        }
    })
}

如何将 handleQty 值添加到行状态?

1 个答案:

答案 0 :(得分:0)

和handleInputChange一样吗?

const list = [...row];
const item = list[index];
if (item) {
  item.qty = e.target.value;
  setRow(list);
}

同样在 React 中,改变数据也不是一个好习惯。您应该使用更新后的项目创建新列表。

Whats the best way to update an object in an array in ReactJS?