How can I update my component in real time when clicking?

Webwoman

I'm working on a ReactJS, Redux, and GraphQL stacks' ReactJS component A.

My dataflow is that when on an another component B a user click on a image a specific function it send some data about a document to fetch on my database on component A via GraphQL.

My difficulty is that when I click on the image my ReactJS render with one step behind. Technically is the GraphQL state which update.

How make the rendering simultaneous to the image clicking, which trigger the query ?

Here my component.js:

import React, { Component } from 'react';
import style from "./Displayer.css";

import client from "apollo-client"
import {graphql, Query} from "react-apollo";
import gql from "graphql-tag";

import equal  from 'deep-equal';

class Displayer extends Component { 

    // some data
    backgroundUrl =  {
    foo
    }

    recipe; // declare the recipe variable in order to handle the reception on data ulteriously 

    // my try to render the component immediatly
    shouldComponentUpdate (nextProps) {
    const currentRecipe = this.props.data.recipe;
    const nextRecipe = nextProps.data.recipe;

    // deep check if the previous values and next values are similars
   // if not, rerender the component
    if (!equal(currentRecipe,  nextRecipe)) {
    if(this.props.data.recipe) { 
        var {title, description} =    this.props.data.recipe
        return this.recipe=  (
            <div className={style.dish_details} >
                <p> Title: {title}   </p>
                <p> Description: {description}  </p>
            </div>
        ) 
        // if there is no recipe data, just display a simple text
        }else{ 
        return this.recipe= <div>  Here details </div>
        }
     // if the values are similars don't rerender the component
    }else {
        return false;
    }
    }

    render() {       
        return ( 
          <div>   
                 // render the recipe
                 {this.recipe}
          </div>
        )
    }
}


// set the query 
const fetchRecipe = gql`
  query recipe($id: String!) { 
        recipe(id: $id){ 
      title 
      description
    }
  }
`;

// bind GraphQL to my component
export default graphql(fetchRecipe, 
        {options(ownProps) {
            return {
              variables: { id : ownProps.id} //ownProps.id allow to recuperate the Redux's id's state, to make the recipe query
            }
}})(Displayer); 

I can't figure out what is wrong. If anybody have an hint, would be great.

remix23

The "one step behind issue", common in react, is almost always because you are accessing this.props instead of nextProps. What if you rewrite your test this way:

if(nextProps.data.recipe) { 
    var {title, description} =    nextProps.data.recipe
    return this.recipe=  (
        <div className={style.dish_details} >
            <p> Title: {title}   </p>
            <p> Description: {description}  </p>
        </div>
    ) 
    // if there is no recipe data, just display a simple text
    }else{ 
    return this.recipe= <div>  Here details </div>
    }
 // if the values are similars don't rerender the component
}else {
    return false;
}

?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

When embedding PageDown editor in my webpage, how I can I stop it from doing conversion in real time?

From Dev

When embedding PageDown editor in my webpage, how I can I stop it from doing conversion in real time?

From Dev

How can I update my app UI when time( in hours) change

From Dev

How can I update my React component state?

From Dev

How can I update data from parent component when I click child component on the vue component?

From Dev

How can I update my GUI score one point at a time?

From Dev

How can I update my GUI score one point at a time?

From Dev

While my app is running, how can I update the current activity with the contents a GCM notification, without clicking on the notification?

From Dev

How can I disable 'Data Analysis in Real Time component' in windows 2012 R2

From Dev

How can i pass and update in real time from a new form the pictureBox1 in form1?

From Dev

How can i update in live real time listBox adding items from another class?

From Dev

How can i update the DirectoryInfo array variable in backgroundworker dowork event in real time?

From Dev

How can I update a page in real time after receiving a POST request in PHP?

From Dev

How can I automatically update a filter when my analysis is opened?

From Dev

How can I prevent a button from clicking a second time when it already has an Onclick event in C#

From Dev

How can I know when my screen was locked last time?

From Dev

How can I know when my screen was locked last time?

From Dev

How can I pass data in real time to my PC running a python script via an Arduino Bluetooth module?

From Dev

How can I get the selected value in the real-time, when I'm scrolling the UIPickerView

From Dev

How can I get my search box tabs to remain active when clicking between them?

From Dev

How can I make my caret stop from moving when clicking on dropdown menu?

From Dev

how do i update time in my program?

From Dev

how do i update time in my program?

From Dev

How do I get the value of a slider to update in real-time?

From Dev

How can I update my nested component's state using mount()?

From Dev

How can i change background color of my layout by clicking a Button

From Dev

How can i update my serviceworker script to update the cached pages when changes are made?

From Dev

How can i update my serviceworker script to update the cached pages when changes are made?

From Dev

How can I update the time from chosen timezone every minute on my webpage?

Related Related

  1. 1

    When embedding PageDown editor in my webpage, how I can I stop it from doing conversion in real time?

  2. 2

    When embedding PageDown editor in my webpage, how I can I stop it from doing conversion in real time?

  3. 3

    How can I update my app UI when time( in hours) change

  4. 4

    How can I update my React component state?

  5. 5

    How can I update data from parent component when I click child component on the vue component?

  6. 6

    How can I update my GUI score one point at a time?

  7. 7

    How can I update my GUI score one point at a time?

  8. 8

    While my app is running, how can I update the current activity with the contents a GCM notification, without clicking on the notification?

  9. 9

    How can I disable 'Data Analysis in Real Time component' in windows 2012 R2

  10. 10

    How can i pass and update in real time from a new form the pictureBox1 in form1?

  11. 11

    How can i update in live real time listBox adding items from another class?

  12. 12

    How can i update the DirectoryInfo array variable in backgroundworker dowork event in real time?

  13. 13

    How can I update a page in real time after receiving a POST request in PHP?

  14. 14

    How can I automatically update a filter when my analysis is opened?

  15. 15

    How can I prevent a button from clicking a second time when it already has an Onclick event in C#

  16. 16

    How can I know when my screen was locked last time?

  17. 17

    How can I know when my screen was locked last time?

  18. 18

    How can I pass data in real time to my PC running a python script via an Arduino Bluetooth module?

  19. 19

    How can I get the selected value in the real-time, when I'm scrolling the UIPickerView

  20. 20

    How can I get my search box tabs to remain active when clicking between them?

  21. 21

    How can I make my caret stop from moving when clicking on dropdown menu?

  22. 22

    how do i update time in my program?

  23. 23

    how do i update time in my program?

  24. 24

    How do I get the value of a slider to update in real-time?

  25. 25

    How can I update my nested component's state using mount()?

  26. 26

    How can i change background color of my layout by clicking a Button

  27. 27

    How can i update my serviceworker script to update the cached pages when changes are made?

  28. 28

    How can i update my serviceworker script to update the cached pages when changes are made?

  29. 29

    How can I update the time from chosen timezone every minute on my webpage?

HotTag

Archive