1

I am displaying table from API, so when I click to delete it should delete

Now it's deleting actually. but the problem is it's not rendering the output

Here is my code for that function

  const delpersonHandler = index => {
    const apiDel = api;
    console.log(index);
    api.splice(index, 1);
    console.log(api);
    setApi({ ...api, apiDel });
  };

here is where i call that

 <TableCell align="Justify">
   <Button variant="outlined">Edit</Button>
   <Button variant="outlined" onClick={() => delpersonHandler(id)}>
      Delete
   </Button>
 </TableCell>

full code is available here

https://pastebin.com/u7fAefBH

3
  • that api is the state... ( useState ) Commented Feb 29, 2020 at 19:00
  • I Found this as answer, any other good method ? const temp = [...api]; temp.splice(id, 1); setApi(temp); console.log("state updated", api); Commented Feb 29, 2020 at 19:29
  • The above will work well. Commented Feb 29, 2020 at 19:33

3 Answers 3

3

React states are immutable hence doing api.splice(index, 1); does not work because you're directly affecting the React state.

api is an array but you're setting an object in the setApi setter function

the simplest way to do this is

    const delpersonHandler = index => {
        let oldApi = [...api] // new array containing api's elements
        oldApi.splice(index,1); 
        setApi(oldApi);
      };
Sign up to request clarification or add additional context in comments.

1 Comment

Yep.. Refer my answer
2

Assuming you are working with functional components, the useState hook delivers two values, specifically the getter and setter of your state, invoking the last one with some parameter will set a new value and invoke a render call.

In the example, setApi is this setter method, but you are calling it with an object as parameter instead an array. And by using the splice method with the api value is possible to inferer that it must be an array. So you need to call the setter with the same variable type:

const delpersonHandler = index => {
  // Don't apply changes directly, instead clone it and modify it.
  const newApi = api.slice();
  newApi.splice(index, 1);

  // Pass an array instead an object
  setApi(newApi); 
};

4 Comments

"the useState hook delivers two values, specifically the getter and setter of your prop" - useState, as implied by the name, is used for state, not props.
i followed your code.. its deleting, but its not rendering.. imgur.com/a/bfDTxgJ
@LogeshP Did you receive some warning in the console?
pls see the image.. i attached here on previous comment
1

I found the fix by using the below code

const delpersonHandler = index => {
const apiDel= [...api]; //getting the current state of array using spread operator
apiDel.splice(index, 1);  //deleting based on index
setApi(apiDel);
console.log("state updated", api);
}

[...api] without this line this wasn't working

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.