Day 7: React Components & Firebase

Thursday, July 5, 2018

Lecture Videos

Morning:

Afternoon:

Topics

React

JavaScript

Firebase

Examples

React

Methods as props

Sometimes one component needs to update another component’s state. It can’t do that directly, but it can call a method from that other component if it’s available via a prop.


  
import React from 'react'
import ReactDOM from 'react-dom'

const PartyButton = ({ celebrate, celebrations }) => {
  return <button onClick={celebrate}>Party! {celebrations}</button>
}

class App extends React.Component {
  constructor() {
    super()
    this.state = {
      celebrations: 0,
    }
    this.celebrate = this.celebrate.bind(this)
  }

  celebrate() {
    const celebrations = this.state.celebrations + 1
    this.setState({ celebrations })
  }

  render() {
    return <PartyButton celebrate={this.celebrate} celebrations={this.state.celebrations} />
  }
}

ReactDOM.render(<App />, document.querySelector('main'))

  
  

JavaScript (ES6+)

Destructuring assignment

Destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.



const myObj = {
  a: true,
  b: 'Destructuring!'
}

let { a, b } = myObj

console.log(a) // => true
console.log(b) // => 'Destructuring!'


Firebase

Database Setup / Auth

base.js


  
import firebase from 'firebase/app'

// Initialize Firebase
const config = {
  apiKey: "YOUR API KEY",
  authDomain: "YOUR AUTH DOMAIN",
  databaseURL: "YOUR DATABASE URL",
  projectId: "YOUR PROJECT ID",
  storageBucket: "YOUR STORAGE BUCKET",
  messagingSenderId: "YOUR MESSAGING SENDER ID"
}

const app = firebase.initializeApp(config)

  
  

Firebase isn’t just a real-time database. It can also provide authentication services via email/password, phone, or common third-party services like Github, Facebook, and Google. Read more about authentication using Firebase here.

Projects

Chatarang

Homework

ALL HYPER FIGHTING, ALL THE TIME!

  • Add the re-base package to your project. At the command-line, type:
yarn add re-base

Confirm that re-base is now listed under dependencies in package.json.

  • Update base.js to initialize and export Rebase.

Hint: export default Rebase.createClass(db)

Super Mega Bonus Credit Hyper Fighting

  • Try to make authentication work with Firebase!