JavaScript FrameworksReact

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

Part 2: For this exercise, we’re going to refactor what we did in Part 1 and move the flight list table to a child component. We’ll then pass the flight data as props to the child component to render. We also pass the deleteFlight() function to the child component in order for it to send data back up to the parent to update the state.

The final state of our root component.

import React from 'react';
import './App.css';
import Nav from './components/Nav';
import Footer from './components/Footer';
import NavData from './models/NavData';
import FlighData from './models/FlightData';
import FlightList from './components/FlightList';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { navData: NavData, flightData: FlighData };
    this.deleteFlight = this.deleteFlight.bind(this);
  }

  deleteFlight(id) {
    let temp = this.state.flightData;
    let index = temp.findIndex( flight => flight.id == id);
    temp.splice(index,1);
    this.setState({flightData: temp});
    console.log(id);
  }
  render() {
    return (
      <div className="App">
        <Nav navData={this.state.navData} />
        <FlightList flightData={this.state.flightData} deleteFlight={this.deleteFlight} />      
        <Footer />
      </div>
    );
  }
}

export default App;

The final state of the child component. We’ve created two additional child components.

import React from 'react';

let FlightList = (props) => {
    return (
        <div className="container">
            <div className="row mb5">
                <h1 className="text-left">Flight Schedule</h1>
                <div className="table-responsive border p-4 bg-light rounded">
                    <p className="text-left font-weight-bold">Active Flight Schedule</p>
                    <table className="table table-hover">
                        <TableHead />
                        <TableBody flightData={props.flightData} deleteFlight={props.deleteFlight}/>
                    </table>
                </div>
            </div>

        </div>

    );
}

let TableHead = () => {
    return (
        <thead className="table-borderless table-secondary">
            <tr>
                <th scope="col">No.</th>
                <th scope="col">Airline</th>
                <th scope="col">Flight Number</th>
                <th scope="col">Trip Type</th>
                <th scope="col">Departure Airport</th>
                <th scope="col">Arrival Airport</th>
                <th scope="col">Departure Date</th>
                <th scope="col">Arrival Date</th>
                <th scope="col" style={{ width: '150px' }}>Actions</th>
            </tr>
        </thead>
    )
}

let TableBody = (props) => {
    return (
        <tbody>
            {props.flightData.map(flight => {
                return (
                    <tr key={flight.id}>
                        <td scope="row">{flight.id}</td>
                        <td>{flight.airline}</td>
                        <td>{flight.flight_no}</td>
                        <td>{flight.trip_type}</td>
                        <td>{flight.departure_airport}</td>
                        <td>{flight.arrival_airport}</td>
                        <td>{flight.departure_date}</td>
                        <td>{flight.return_date}</td>
                        <td>
                            <a type="button"
                                className="btn btn-success btn-just-icon btn-sm"
                                href={"/edit/" + flight.id}>Edit</a>

                            <button onClick={() => {
                                props.deleteFlight(flight.id)
                            }}
                                type="button"
                                className="btn btn-sm btn-danger ml-2">
                                Delete
                        </button>
                        </td>
                    </tr>
                );
            })

            }

        </tbody>
    );
}


export default FlightList;
Verified by MonsterInsights