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
  • Pulling In Data
  • Plan of Attack
  • Make It Look Nice!
  1. Part 8: Apps
  2. 8.0 - Bitcoin Api Application

8.5 - Bitcoin API Info Box

Now that we have a sweet looking graph, let's add some information on top.

First thing's first, we need to link it properly. Repeat the same method as you did for LineChart.js, importing both React and Component. And, in Bitcoin.js, add the same data attribute in your JSX tag for InfoBox as you did for LineChart.

Now, add this to your InfoBox.js file:

import React, { Component } from 'react';

export default class InfoBox extends Component {
  constructor(props) {
    super(props);
    this.state ={
      payout: []
    }
  }

  render() {
    return (
      <div>
        <div>
          <div>
            <h3>Current Price:</h3>
          </div>
          <div>
            <h5>Current</h5>
          </div>
        </div>
        <div>
          <div>
            <h3>Change Since Last Month (USD):</h3>
          </div>
          <div>
            <h5>Payout</h5>
          </div>
        </div>
      </div>
    );
  }
};

We are repeating similar concepts that we encountered in LineChart.js; it is simply not including dates (we don't need them here), so we are just going to move on. You should see something like this:

Pulling In Data

Now, let's pull in our componentDidMount() (very, very similar to what we saw in LineChart.js; basically identical, in fact):

componentDidMount() {
  const infoData = this.props.data;
  let payout = [];
  for (let thing in infoData) {
    payout.push(infoData[thing])
  }
  this.setState({
    payout: payout
  })
}

This is the exact same as what we saw in our LineChart.js file, save dates is gone; otherwise, it is all the same of what we have seen before.

Plan of Attack

What are we trying to do here? The first value we are trying to find is the current price of bitcoin. This is relatively simple, considering we are working with an array. The second value we are trying to identify is the difference within the month, effectively finding the value from subtracting the current price from the payout a month ago; again, relatively simple.

To find the first value, we simply pull out the 30th index of our array (being that it is the last value listed and therefore our most-recent). We already have created an array of the price called payout. And, to access the 30th index of the array, we write payout[30]. But what do we do from there? Let's use our state.

To begin, add a value in our state up at the top called infoCurrent and set it to an empty array, like the others. In our setState(), write this: infoCurrent: payout[30]. And then change your value within your h5 tag from current to { this.state.infoCurrent }. You should now see this:

Great! Now let's fix our second value--the change in price over the course of the month. Again, we already have the value, so we need to subtract the current value of payout (payout[30]) from the first payout[0]. This will show us the difference over the month.

Again, in our state, let's add another value: infoPayout and set it, like all the others, to an empty array. In our setState(), write the following: infoPayout: payout[30] - payout[0]. Then, to see it, replace the value of your h5 tag from payout to { this.state.infoPayout }. You should now see this:

Make It Look Nice!

Our values are all here now! However, it looks ugly again. So, we should change the values so they make sense. Let's change our new variables to show currency, rather than just numbers. In your setState(), in your infoCurrent value, append .toLocaleString('us-EN', { style: 'currency', currency: 'USD' }) after your payout[30]. You should now see currency under your Current Price!

But what happens when we append this to the end of the other value? It throws an NaN. Why? Because it is converting the numeric value of the payout[0] to currency before they are subtracted! A simple fix to this is to wrap payout[30] - payout[0] in parentheses; that should fix the problem.

Great! You have currency instead of just boring old numbers! But it still looks pretty bad. Let's add some syled components to this. First, import this: import styled from 'styled-components';. Next, right under your imports, add the following:

const InfoCard = styled.div `
  display: inline-block;
  width: 50%;
  margin-bottom: 1em;
  text-align: center;
  color: #d9514e;
`

And, lastly, we need to apply our new styling. In the JSX, adjust the following:

render() {
  return (
    <div>
      <InfoCard>
        <div>
          <h3>Current Price:</h3>
        </div>
        <div>
          <h5>{this.state.infoCurrent}</h5>
        </div>
      </InfoCard>
      <InfoCard>
        <div>
          <h3>Change Since Last Month (USD):</h3>
        </div>
        <div>
          <h5>{ this.state.infoPayout }</h5>
        </div>
      </InfoCard>
    </div>
  );
}

You should now be able to see this:

Previous8.4 - Bitcoin API Fetching DataNext8.6 - Bitcoin API Completed Code

Last updated 6 years ago

picture
picture
picture
picture