0

I am creating the list from the skidList array and handling copy and delete operation on each skid as well as on click of delete button I am calling deleteSkid method which removes skid at that index and updates the list.

However, Instead of deleting skid at a particular index, it deletes the last skid in the array list.

enter image description here

const CreateNewTab = () => {
  const [skidList, setSkidList] = useState([]);
  const [productNameMap, setproductNameMap] = useState({});

  useEffect(() => {
    let skidList = [];
    let newSkid = {};
    newSkid["2"] = "0";
    newSkid["3"] = "0";
    newSkid["4"] = "0";
    skidList.push(newSkid);
    setSkidList([...skidList]);
    let productNameMap = {};
    productNameMap["2"] = "PEN";
    productNameMap["3"] = "PENCIL";
    productNameMap["4"] = "ERASER";
    setproductNameMap({ ...productNameMap });
  }, []);

  const updateProductQuantity = (skid, key, newQuantity, index) => {
    console.log("Inside update Skid Quantity Index= " + index);
    skid[key] = newQuantity;
    let newSkidList = skidList;
    newSkidList.splice(index, 1, skid);
    console.log(newSkidList);
    setSkidList([...newSkidList]);
  };

  const deleteSkid = (index) => {
    console.log(index);
    let newSkidList = skidList;
    newSkidList.splice(index, 1);
    console.log("Skid deleted from: " + newSkidList);
    setSkidList([...newSkidList]);
  };
  const insertSkid = (skid) => {
    setSkidList(skidList.concat([{ ...skid }]));
  };
  return (
    <div>
      {skidList.flatMap((skid, index) => (
        <div style={{ marginRight: "0", paddingRight: "0" }}>
          <Row style={{ margin: "0", padding: "0" }}>
            <Col span={19}>
              {Object.keys(skid).map((key) => (
                <Row>
                  <Col span={16}>
                    <h6>{productNameMap[key.toString()]}</h6>
                  </Col>
                  <Col span={8}>
                    <InputNumber
                      min={0}
                      defaultValue={skid[key]}
                      rules={[
                        {
                          required: true,
                          message: "Please input quantity!",
                        },
                      ]}
                      onChange={(newQuantity) => {
                        updateProductQuantity(skid, key, newQuantity, index);
                      }}
                    />
                  </Col>
                </Row>
              ))}
            </Col>
            <Col span={5}>
              <Row>
                <Col span={12}>
                  <Button
                    type="primary"
                    icon={<CopyOutlined />}
                    size="large"
                    shape="circle"
                    onClick={() => insertSkid(skid)}
                  />
                </Col>
                <Col spna={12}>
                  <Button
                    type="primary"
                    size="large"
                    shape="circle"
                    danger
                    icon={<DeleteOutlined />}
                    onClick={() => deleteSkid(index)}
                  />
                </Col>
              </Row>
              }
            </Col>
          </Row>
          <Divider />
        </div>
      ))}
    </div>
  );
};
8
  • 1
    why are you using .flatMap? Commented Oct 24, 2020 at 4:08
  • I tried using .map and having the same issue. Commented Oct 24, 2020 at 4:10
  • have you tried using key prop on parent element inside flatMap like this <div key={index} style={{ marginRight: "0", paddingRight: "0" }}> ? Commented Oct 24, 2020 at 4:11
  • Yeah, I tried using key prop as you have suggested. It doesn't solve the issue. Commented Oct 24, 2020 at 4:15
  • Does this answer your question? What is the cleanest way to remove an element from an immutable array in JS? Commented Oct 24, 2020 at 4:15

2 Answers 2

1

It seems it has something to do in how react recycles the dom or something, the item does get deleted from the array but somehow the value is kept in the dom because you're using defaultValue instead value in your inputs, make this little change and it will work

value={skid[key]}

https://codesandbox.io/s/compassionate-ride-1yoti?file=/src/App.js

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @diedu. You made my day! I would love to grab you a coffee if you're in Toronto or visiting in the future.
1

Your implementation of deleteSkid() is fine, But you pass the value to the InputNumber as defaultValue so it won't change when the component re-render. Just replace it with value and it should work.

<InputNumber
  min={0}
  value={skid[key]}
  rules={[
    {
      required: true,
      message: "Please input quantity!"
    }
  ]}
  onChange={(newQuantity) => {
    updateProductQuantity(skid, key, newQuantity, index);
  }}
/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.