JS-201-ReactFundamentals
  • Part 0: App Overview
  • Part 1: Intro to React
    • 1.0: Packages Setup
    • 1.1: Project Setup
    • 1.2: React Router Dom
  • Part 2: JavaScript Concepts
    • 2.0: ES6 Overview
    • 2.1: classes
    • 2.2: constructors
    • 2.3: extends
    • 2.4: super
    • 2.5: interpolation
    • 2.6: map
    • 2.7: filter
  • Part 3: Functional Components
    • 3.0: Functional Components Overview
    • 3.1: Calling Functional Components
    • 3.2: Single Element Rule
    • 3.3: Arrow Functions
    • 3.4: Challenge
    • 3.4: Solution
    • 3.5: Challenge 2
    • 3.5: Solution 2
  • Part 4: JSX Challenge
    • 4.1: JSX Overview
    • 4.1: Challenge Answer
    • 4.2: className
  • Part 5: Class Concepts
    • 5.1: State
    • 5.2: setState
    • 5.3: Class Components Challenge
    • 5.4: Class Components Challenge Answer
  • Part 6: Props Starter
    • 6.0: Props Overview
    • 6.1: Props Demo and Challenge 1
    • 6.2: Props Answer 2
    • 6.3: Props Passing Functions and Challenge 2
    • 6.4: Props Answer 2
    • 6.5: External Props and Mapping Components
    • 6.6: PropTypes
  • Part 7: Lifecycle Methods
    • 7.0: Lifecycle Overview
    • 7.1: Lifecycle Methods
    • 7.2: Mounting Methods
    • 7.3: Update Methods
    • 7.4: Unmount Methods
  • Part 8: Apps
    • 1.0 - Small Timer Apps
      • 1.1 - Simple Timer
      • 1.2 - Clock App
      • 1.3 - Stop Watch App
      • 1.4 - Challenge
    • 2.0 - Concept List
      • 2.1 - Concept Data
      • 2.2 - Concept App Component
      • 2.3 - Concept Component
      • 2.4 - Concept List Component
    • 3.0 - NYT
      • 3.1 - NytApp Component
      • 3.2 - Nyt Results
    • 4.0 - The Friend List App
      • 4.1 - Friend List App Component
      • 4.2 - Friend Component
    • 5.0 - Movie Search Application
      • 5.1 - Form Component
      • 5.2 - Form Results Component
      • 5.3 - Styled Components
    • 6.0 - YouTube Api
      • 6.1 - Video Component
      • 6.2 - Video Component
      • 6.3 - Video Component
    • 7.0 - Github Api Application
      • 7.1 - The Users
      • 7.2 - Github API Component
      • 7.3 - Github API Card
      • 7.4 - Github API Card Form
      • 7.5 - Github API Card List
      • 7.6 - Github API Search
      • 7.7 - Github API Challenge
    • 8.0 - Bitcoin Api Application
      • 8.1 - Bitcoin API Setup
      • 8.2 - Bitcoin API Fetch
      • 8.3 - Bitcoin API Line Chart
      • 8.4 - Bitcoin API Fetching Data
      • 8.5 - Bitcoin API Info Box
      • 8.6 - Bitcoin API Completed Code
    • 9.0 - Google Maps Api Challenge
    • 10.0 - Sound Cloud App Challenge
    • 11.0 - VR App Challenge
    • 12.0 - React Native Intro
  • Part 9: Project
  • Part 10: Notes
    • 10.1 - Resources
    • 10.2 - Troubleshooting
Powered by GitBook
On this page
  • Friend
  • Mapping to Friend
  • Delete Friend
  • componentDidMount
  • Final Code
  1. Part 8: Apps
  2. 4.0 - The Friend List App

4.2 - Friend Component

We'll now add another component for rendering a friend. This is the component that we referred to at the end of the last module before the final code block. Now, let's implement it.

Friend

We have the data firing, but we have no way of showing it. We need to show the data in a list. Let's stub out everything we'll need here. We'll code with intent here, thinking about what we'll want to be able to do starting with creating a new file called FriendList.js inside the same directory as FriendListApp.js

Go into the Friend.js file and add the following.

import React from 'react';
import FaTrashO from 'react-icons/lib/fa/trash-o'

const Friend = () => {
  return (
    <div>
      Friend
    </div>
  )
}

export default Friend;

All we want this component to do is render each friend. All of the remaining logic that we still need to implement will need to happen back in our FriendListApp component, as we're keeping this one very simple. All we need to do here is figure out how to render each friend. Let's go ahead and do that how we want to. This will be our finished Friend component.

import React from 'react';
import FaTrashO from 'react-icons/lib/fa/trash-o'

const Friend = ({friend, removeFriend}) => {
  return (
    <div>
      <li className="list-group-item">
          <strong>Name:</strong> {friend.name}
          <br />
          <strong>Age:</strong> {friend.age}
          <button onClick={(e) => {removeFriend(e, friend) }}
              className="btn btn-danger trash">
              <span><FaTrashO /></span>
          </button>
      </li>
    </div>
  )
}

export default Friend;

Notice that we're passing two props, one holds the friend information and we're calling it friend, the other is the removeFriend function, that will theoretically remove our friend. We haven't implemented this stuff in our FriendListApp component yet so we need to do that still.

Mapping to Friend

We need to iterate through all of the friends in our state and map them out to our Friend component! This is a very common pattern you'll see in React, where you have a large dataset, and then you map it out into functional components. So here we need to do that. Inside the render, underneath the form add the following:

</form>

**** add here ****
{this.state.friends.map(friend => <Friend friend={friend} key={friend.id} />)}
**** stop adding ****
</div>

All we are doing here is mapping our state out to our friend component. Remember when mapping multiples of the same thing, React requires unique key props, so we've added one with id!

Delete Friend

Before, when we created our Friend component we had a removeFriend prop that we used. Let's create that method in our FriendListApp component now. We know based on how we set up Friend that it need to take in an event, and a friend!

Last, we need to update our state. We can do this by filtering out the friend we passed in!

Create the following deleteFriend method in your FriendListApp class, put it underneath and outside of your constructor but above your render!

deleteFriend = (e, friend) => {
    fetch(`${API_BASE}/${friend.id}`, {
      method: "DELETE",
      headers: {
        "Content-Type": "application/json",
      }
    })
    .then(res => {
      let tempFriends = this.state.friends.filter(fr => fr !== friend)
      this.setState({friends: tempFriends})
    })
  }

Cool, now we just need to pass it to our Friend component to be able to use it! Update your map in your render to look like this:

 {this.state.friends.map(friend => <Friend friend={friend} key={friend.id} removeFriend={this.deleteFriend}/>)}

Now, try it out!! You should be able to add and delete friends!

componentDidMount

You may have noticed, that we have a problem. Try refreshing the app. You should get the form back, but it won't have the data:

Let's fix this! Go to FriendListApp and let's use componentDidMount. Add a fetch request to get our data when our component mounts! This should get our information when we refresh the page!

componentDidMount(){
    fetch(API_BASE)
    .then(res => res.json())
    .then(response => {
      console.log(response)
      this.setState({friends: response})
    })
  }

Final Code

Here is the code for this module & app:

Friend.js

import React from 'react';
import FaTrashO from 'react-icons/lib/fa/trash-o'

const Friend = ({friend, removeFriend}) => {
  return (
    <div>
      <li className="list-group-item">
          <strong>Name:</strong> {friend.name}
          <br />
          <strong>Age:</strong> {friend.age}
          <button onClick={(e) => {removeFriend(e, friend) }}
              className="btn btn-danger trash">
              <span><FaTrashO /></span>
          </button>
      </li>
    </div>
  )
}

export default Friend;

FriendListApp.js

import React, { Component } from "react";
import Friend from './Friend'

const API_BASE = "http://rest.learncode.academy/api/efa/friends";

class FriendListApp extends Component {
  constructor(props) {
    super(props);
    this.state = { friends: [] };
  }

  componentDidMount(){
    fetch(API_BASE)
    .then(res => res.json())
    .then(response => {
      console.log(response)
      this.setState({friends: response})
    })
  }

  handleSubmit = event => {
    event.preventDefault();
    var name = this.refs.name.value;
    var age = this.refs.age.value;
    var friendsTemp = this.state.friends;

    fetch(API_BASE, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ name: name, age: age })
    })
      .then(res => res.json())
      .then(response => {
        console.log(response)
        friendsTemp.push(response)
        this.setState({ friends: friendsTemp })
        this.refs.name.value = ""
        this.refs.age.value = ""
      })
  }

  deleteFriend = (e, friend) => {
    fetch(`${API_BASE}/${friend.id}`, {
      method: "DELETE",
      headers: {
        "Content-Type": "application/json",
      }
    })
    .then(res => {
      let tempFriends = this.state.friends.filter(fr => fr !== friend)
      this.setState({friends: tempFriends})
    })
  }

  render() {
    return (
      <div className="main">
        <div className="mainDiv">
          <form onSubmit={this.handleSubmit.bind(this)}>
            <h3>Enter a Friend!</h3>
            <fieldset className="form-group">
              <label>Friend's Name:</label>
              <input
                type="text"
                ref="name"
                name="name"
                className="form-control"
              />
            </fieldset>

            <fieldset className="form-group">
              <label>Friend's Age:</label>
              <input
                type="text"
                ref="age"
                name="age"
                className="form-control"
              />
            </fieldset>
            <button className="btn btn-success" type="submit">
              Save Friend
            </button>
          </form>
          {this.state.friends.map(friend => <Friend friend={friend} key={friend.id} removeFriend={this.deleteFriend}/>)}
        </div>
      </div>
    );
  }
}

export default FriendListApp;

Hopefully, you guys learned more about props and state from this app! Also practice with using APIs and react!

Previous4.1 - Friend List App ComponentNext5.0 - Movie Search Application

Last updated 7 years ago

Next, we can check out the API documentation and see the structure for how we can make a delete request!

here
refresh