Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2. import { Router } from "@reach/router"; // Remember this import when using <Router>
  3. import Recipe from "./components/Recipe ";
  4.  
  5. class App extends Component {
  6.   constructor(props) {
  7.     super(props);
  8.  
  9.     // This is my state data initialized
  10.     this.state = {
  11.       recipes: [
  12.         // descriptions could be better...
  13.         { id: 1, title: "Pizza", description: "How to make pizza..." },
  14.         { id: 2, title: "Soup", description: "How to make soup..." },
  15.         { id: 3, title: "Cake", description: "How to make cake..." }
  16.       ]
  17.     };
  18.  
  19.     console.log(this.state.recipes.find(e => e.id === Number(3)));
  20.   }
  21.  
  22.   getRecipe(id) {
  23.     // Basically, go over all elements in 'this.state.recipe' and find the element
  24.     // that matches 'e.id === Number(id)' where 'e' is one of the objects in 'this.state.recipes'
  25.     return this.state.recipes.find(e => e.id === Number(id)); // And then return it
  26.   }
  27.  
  28.   render() {
  29.     return (
  30.       <React.Fragment>
  31.         <h1>Recipe App!</h1>
  32.         <Router>
  33.           {/* Writing JavaScript comments inside JSX is a bit weird as you can see */}
  34.  
  35.           {/* When using 'this.props.loadRecipe()' from inside Recipe, it actually uses this
  36.                         arrow function below */}
  37.           <Recipe path="/recipe/:id" loadRecipe={id => this.getRecipe(id)} />
  38.  
  39.           {/* <Recipes> below has not been implemented yet, so it is commented out*/}
  40.           {/*<Recipes path="/recipes" recipes={this.state.recipes}></Recipes>*/}
  41.         </Router>
  42.       </React.Fragment>
  43.     );
  44.   }
  45. }
  46.  
  47. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement