0

I am trying to update a row in react table using redux logic.The following is the action that I am calling in the component.

handleEdit = () => {
    this.setState({
      id: this.state.id,
      name: this.state.name,
      uid: this.state.uid,
      serial: this.state.serial,
      name: this.state.name,
      status: this.state.status,
      nodeType: this.state.nodeType
    });

    this.props.editCard({
      id: this.state.id,
      name: this.state.name,
      uid: this.state.uid,
      serial: this.state.serial,
      name: this.state.name,
      status: this.state.status,
      nodeType: this.state.nodeType
    });

  };

This is how I wrote the reducer

[types.CARD_UPLOADER_EDIT_SUCCESS]: (state, { payload }) => {
      console.log("payload", payload);
      const index =
        state.data && state.data.findIndex(i => i.id === payload.id);
      if (index != -1 && state.data) {
        state.data.splice(index, 1, payload);
      }
      return { ...state, editSuccess: true };
    },

Although the service was fired , instead of updation, a new entry will be added to the database. Can someone help me for sorting this out?

Ruks
  • 75
  • 1
  • 9
  • I think the issue is in your DB update logic. You have to update if the id already exist. – SelvaS Jun 12 '19 at 06:30
  • @SelvaTS I have added the reducer in the bottom of the question – Ruks Jun 12 '19 at 06:37
  • Even though you are updating the state, but you are passing the same id. May be your database update not validated the existing record. – SelvaS Jun 12 '19 at 06:41

0 Answers0