简体   繁体   English

如何循环遍历数组并使用映射向 React 项目添加一个数字

[英]how can I loop through an array and add a number to the item React using mapping

I am trying to loop through an array in react and trying to add 1(or any other value) to the value of "mktratedelta" with the "handleplus" function I made.我试图在反应中循环一个数组,并尝试使用我制作的“handleplus”function 将 1(或任何其他值)添加到“mktratedelta”的值。 I also created a button which calls this function "handleplus".我还创建了一个按钮,该按钮将此 function 称为“handleplus”。 But whenever try to click on the button i get "NaN" value in the field.但是每当尝试单击按钮时,我都会在该字段中获得“NaN”值。 How can i fix this issue and is this the right approach?我该如何解决这个问题,这是正确的方法吗?

function TableExampleStriped() {
  const [marketEstimateDataBCAssets, setmarketEstimateData] = useState([
    {
      name: "BcLombard",
      prevgroupinputrate: 0.01,
      currgroupinputrate: 12.0,
      mktratedelta: 4.0,
      mktrateestimate: 20.0
    },
    {
      name: "BcOtherSecured",
      prevgroupinputrate: 0.01,
      currgroupinputrate: 17.0,
      mktratedelta: 3.0,
      mktrateestimate: 10.0
    },
    {
      name: "BcUnsecured",
      prevgroupinputrate: 0.01,
      currgroupinputrate: 7.0,
      mktratedelta: 2.0,
      mktrateestimate: 10.0
    }
  ]);

  const handleChange = item => e => {
    const newArr = marketEstimateDataBCAssets.map(el => {
      if (el.name === item.name) {
        return {
          ...el,
          mktratedelta: parseFloat(e.target.value),
          mktrateestimate: (
            parseFloat(e.target.value) + item.currgroupinputrate
          ).toFixed(2)
        };
      }
      return el;
    });

    return setmarketEstimateData([...newArr]);
  };

  const handleplus = item => e => {
    const newArr = marketEstimateDataBCAssets.map(el => {
      if (el.name === item.name) {
        return {
          ...el,
          mktratedelta: parseFloat(e.target.value) + 1.0
        };
      }
      return el;
    });

    return setmarketEstimateData([...newArr]);
  };

 <Table.Cell>
                {marketEstimateDataBCAssets.map(item => {
                  if (item.name === "BcOtherSecured") {
                    return <button onClick={handleplus(item)} />;
                  }
                })}
 </Table.Cell>

<Table.Cell>
                {marketEstimateDataBCAssets.map(item => {
                  if (item.name === "BcOtherSecured") {
                    return (
                      <input
                        value={item.mktratedelta}
                        onChange={handleChange(item)}
                      />
                    );
                  }
                })}
</Table.Cell>

For passing data in onClick event of react you need to use the arrow function要在 onClick 事件中传递数据,您需要使用箭头 function

Like below像下面

return <button onClick={(e) => this.handleplus(item)} />;

Please refer the react documentation for handling events here请参阅反应文档以在此处处理事件

I do not see any value present at button like it is at input tag, so whenever it tries to check e.target.value onButton click it will give an undefined and If we do parseFloat(undefined) it will return NaN .我没有看到按钮上存在任何值,就像它在输入标签上一样,所以每当它尝试检查 e.target.value onButton click 时,它会给出一个未定义的值,如果我们执行parseFloat(undefined) 它将返回 NaN you are passing item in onclick, you can fetch required value from it that is present in it.您在 onclick 中传递项目,您可以从中获取所需的值。

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

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