Guest User

Untitled

a guest
Aug 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import style from "./Displayer.css";
  3.  
  4. import client from "apollo-client"
  5. import {graphql, Query} from "react-apollo";
  6. import gql from "graphql-tag";
  7.  
  8. import equal from 'deep-equal';
  9.  
  10. class Displayer extends Component {
  11.  
  12. // some data
  13. backgroundUrl = {
  14. foo
  15. }
  16.  
  17. recipe; // declare the recipe variable in order to handle the reception on data ulteriously
  18.  
  19. // my try to render the component immediatly
  20. shouldComponentUpdate (nextProps) {
  21. const currentRecipe = this.props.data.recipe;
  22. const nextRecipe = nextProps.data.recipe;
  23.  
  24. // deep check if the previous values and next values are similars
  25. // if not, rerender the component
  26. if (!equal(currentRecipe, nextRecipe)) {
  27. if(this.props.data.recipe) {
  28. var {title, description} = this.props.data.recipe
  29. return this.recipe= (
  30. <div className={style.dish_details} >
  31. <p> Title: {title} </p>
  32. <p> Description: {this.props.description} </p>
  33. </div>
  34. )
  35. // if there is no recipe data, just display a simple text
  36. }else{
  37. return this.recipe= <div> Here details </div>
  38. }
  39. }else {
  40. return false;
  41. }
  42. }
  43.  
  44. render() {
  45. return (
  46. <div className={style.displayer_layout} >
  47. <div className={style.recipe_image}></div>
  48. <div>
  49.  
  50. // render the recipe
  51. {this.recipe}
  52.  
  53. </div>
  54. <div className={style.dish_footer}>
  55. <button onClick={this.addArticle} className={style.add}></button>
  56. </div>
  57. </div>
  58. )
  59. }
  60. }
  61.  
  62.  
  63. // set the query
  64. const fetchRecipe = gql`
  65. query recipe($id: String!) {
  66. recipe(id: $id){
  67. title
  68. description
  69. }
  70. }
  71. `;
  72.  
  73. // bind GraphQL to my component
  74. export default graphql(fetchRecipe,
  75. {options(ownProps) {
  76. return {
  77. variables: { id : ownProps.id} //ownProps.id allow to recuperated the Redux's id's state, to make the recipe query
  78. }
  79. }})(Displayer);
Add Comment
Please, Sign In to add comment