Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building API Driven React Applications

Building API Driven React Applications

A look at some best practices for APIs in React apps

scottradcliff

March 16, 2018
Tweet

More Decks by scottradcliff

Other Decks in Programming

Transcript

  1. import React, { Component } from ‘react'; import axios from

    'axios'; import Product from ‘./Product’; class App extends Component { constructor() { super(); this.state = { products: []}; } componentWillMount() { axios.get(‘/api/v1/products’, headers: { Authorization: api_token}) .then(response => { this.setState({ products: response.data.products, }); }) .catch(error => alert(error)) } render() { return ( <div> {this.state.products.map(product => { return <Product product={product} /> }) } </div> ); } } const compassModulesContainerElement = document.getElementById('compass-modules'); export default CompassModulesContainer; if (compassModulesContainerElement) { ReactDOM.render(<CompassModulesContainer />, compassModulesContainerElement); }
  2. import React, { Component } from ‘react'; import axios from

    'axios'; import Product from ‘./Product’; class App extends Component { constructor() { super(); this.state = { products: []}; } componentWillMount() { axios.get(‘/api/v1/products’) .then(response => { this.setState({ products: response.data.products, }); }) .catch(error => alert(error)) } render() { return ( <div> {this.state.products.map(product => { return <Product product={product} /> }) } </div> ); } } const compassModulesContainerElement = document.getElementById('compass-modules'); export default CompassModulesContainer; if (compassModulesContainerElement) { ReactDOM.render(<CompassModulesContainer />, compassModulesContainerElement); }
  3. import React, { Component } from ‘react'; import axios from

    'axios'; import Product from ‘./Product’;
  4. import React, { Component } from ‘react'; import axios from

    'axios'; import Product from ‘./Product’; class App extends Component { constructor() { super(); this.state = { products: []}; } componentWillMount() { axios.get(‘/api/v1/products’, headers: { Authorization: api_token }) .then(response => { this.setState({ products: response.data.products, }); }) .catch(error => alert(error)) } render() { return ( <div> {this.state.products.map(product => { return <Product product={product} /> }) } </div> ); } } const compassModulesContainerElement = document.getElementById('compass-modules'); export default CompassModulesContainer; if (compassModulesContainerElement) { ReactDOM.render(<CompassModulesContainer />, compassModulesContainerElement); }
  5. class App extends Component { constructor() { super(); this.state =

    { products: []}; } componentWillMount() { axios.get(‘/api/v1/products’, headers: { Authorization: api_token }) .then(response => { this.setState({ products: response.data.products, }); }) .catch(error => alert(error)) }
  6. import React, { Component } from ‘react'; import axios from

    'axios'; import Product from ‘./Product’; class App extends Component { constructor() { super(); this.state = { products: []}; } componentWillMount() { axios.get(‘/api/v1/products’, headers: { Authorization: api_token }) .then(response => { this.setState({ products: response.data.products, }); }) .catch(error => alert(error)) } render() { return ( <div> {this.state.products.map(product => { return <Product product={product} /> }) } </div> ); } } const compassModulesContainerElement = document.getElementById('compass-modules'); export default CompassModulesContainer; if (compassModulesContainerElement) { ReactDOM.render(<CompassModulesContainer />, compassModulesContainerElement); }
  7. server |- Gemfile |- Gemfile.lock |- README.md |- Rakefile |-

    app/ |- bin/ |- config/ |- config.ru |- db …
  8. componentWillMount() { axios.get(‘/api/v1/products’, headers: { Authorization: api_token }) .then(response =>

    { this.setState({ products: response.data.products, }); }) .catch(error => alert(error)) }
  9. module Api module V1 class ProductsController < ApplicationController def index

    if current_user @products = Product.all render json: @products, status: 200 end end end end end
  10. module Api module V1 class ProductsController < ApplicationController def index

    if current_user @products = Product.all render json: @products, include: :images, status: 200 end end end end end
  11. [ {id: 1, sku: “sku-1234”, price: 1200, images: [ {id:1,

    url: “someplace.jpg”}, {id: 2, url: “someotherplace.jpg”]}, {id: 2, sku: “sku-4321” price: 1300} ]