R
R
rgggwwg2020-11-22 20:16:27
React
rgggwwg, 2020-11-22 20:16:27

Which of the implementations of the method is more efficient, which one is better to use and why?

Actually, here are the methods:
The first option:

deleteItem = (id) =>
    {
        this.setState(
            ({ toDoData })=>
            {
                const dummyState = JSON.parse( JSON.stringify( toDoData ));
                dummyState.splice(toDoData.findIndex(( item ) => item.id === id), 1);
                return{
                    toDoData: dummyState
                }
            }
        )
    }


Second option:
deleteItem = (id) =>
    {
        this.setState
        (
            ({ toDoData })=>
            {
                let indexItem = toDoData.findIndex(( item ) => item.id === id);
                const dummyState = [...toDoData.slice(0, indexItem), ...toDoData.slice(indexItem + 1)]
                return{
                    toDoData: dummyState
                }
            }
        )
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-11-22
@Seasle

deleteItem = (id) => {
    this.setState(({ toDoData }) => ({
        toDoData: toDoData.filter(entry => entry.id !== id)
    }));
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question