JavaScript FrameworksReact

DELETE One Item From a List in React (Two Parts)

In this two-part video series, we’re going to implement a deleteFlight(id) function to remove a record from a property which is a part of the state property. For this exercise, we’re going to remove data from RAM only and not from a permanent data store. The best option for this operation is to use the two JavaScript built-in functions: findIndex() and splice(). First, we will use the findIndex() function to return the index position of a matching id field, then use the splice() function remove the element from the list. Finally, we’ll invoke the setState() function to update the state, thereby, making the state property “impure”. Once the state property is impure, React will re-render the DOM.

The source files are available below.

Part 1: We’ll create the implementation for the deleteFlight() function.

The source file (App.js)

App.js

The JSON data

FlightData.js
Verified by MonsterInsights