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
  • Analysis
  • Constructor
  • Quick Aside
  • Analysis of the State
  • FetchingData
  1. Part 8: Apps
  2. 8.0 - Bitcoin Api Application

8.2 - Bitcoin API Fetch

Next, we need to add the API to our Bitcoin.js file. Add this within your Bitcoin component above render():

componentDidMount() {
  const url = 'https://api.coindesk.com/v1/bpi/historical/close.json';
  fetch(url)
    .then(response => response.json())
    .then(bitcoinData => {
      // console.log(bitcoinData.bpi);
      this.setState({
        data: bitcoinData.bpi,
        fetchingData: false
      })
    })
    .catch(e => {
      console.log(e);
    })
}

Analysis

We are fetching the url from coindesk.com that we have declared and getting our two .then promises back; first the response, and second, the pertinent data.

If you uncomment out the console.log, you should see something like this when you run it:

This is exactly what we want! This api (bitcoinData.bpi) gives us both the dates and the payouts of bitcoin; it is simply formatted uniquely. Where, normally, objects would come back with an object with keys that we could call and get the value of, here, we need to collect both the key and the value (more on that soon).

Constructor

Once, we gather the data, we set the state for both the data and fetching said data. In order to set the state, we need to have the data. So we need to add our constructor above the componentDidMount:

constructor() {
  super();
  this.state = {
    fetchingData: true,
    data: [],
  }
}

Quick Aside

We see constructor() { super();... }; before we have seen constructor(props) { super(props);... }. Why do we not need it here?

The reality is that the constructor lifecycle method only needs to include props within its parentheses when props are actually called within the constructor itself--it is similar to parameters within a function. We only need to include props if the constructor method uses it; otherwise, it assumes props throughout the component and can go unstated. When in doubt, however, feel free to include props in the parentheses of your constructor.

Analysis of the State

Within the state, we see two arguments: fetchingData: true and data: []. The former is a boolean set to true where it fetches the data; this is helpful if the data is slow-coming. We will see it's point in a second. The latter is an empty array that we will push our fetched data from the API, which we will use in the other two files.

FetchingData

We see fetchingData twice: first in the state, where it is set to true (defaulting to actually gathering the data from the API), and in the setState where it is then set to false, showing that, after we fetch the data, we no longer need the fetch() to go out and get it.

Let's make it useful. In your render() lifecycle method, adjust your JSX to the following:

render() {
  return (
    <div className='main'>
      <div className='mainDiv'>
        <h1>30 Day Bitcoin Price Chart</h1>
        { !this.state.fetchingData ? <h3>Info Box</h3> : null }
        { !this.state.fetchingData ? <h3>Line Chart</h3> : null }
      </div>
    </div>
  );
}

We are adding two ternaries that holds the two h3 tags until the data is completely fetched; if it is still fetching, return null; otherwise show both Info Box and Line Chart. You will likely not see much of a difference (if any), but if you have a slow connection or a problem with the API itself, this will come in handy.

Previous8.1 - Bitcoin API SetupNext8.3 - Bitcoin API Line Chart

Last updated 6 years ago

picture