How do I go about making this part of my code reactive? (Meteor + React)

Mabeh Al-Zuq Yadeek

Basically, I want to make my page reactive and give results based on user's input/choices.

I pass user's choices from parent to child component via props (dictionary is an object with user's choices and amount of choices per key (ex: dictionary = {Gallileo: 2, Juno: 1, Mars: 1}):

<Childcomp aggregate={this.props.dictionary}/>

On the child component, I do a simple loop and the choice and number of votes into a state variable via componentWillMount() method:

  componentWillMount(){
    let obj = this.props.aggregate // {Gallileo: 2, Juno: 1, Mars: 1}
    let choice;
    for(let key in obj){
      if(key === this.props.option){
        choice = obj[key];
        break;
      }
    };

    let total = 0;
    for(let key in obj){
      total += obj[key];
    }

  this.setState({choice: choice, total: total});

};

The problem I have with this is that the state variable does not change the value when input is received. this.props.aggregate does change reactively, but everything after that doesn't respond.

I tried using Session variables to make it reactive, I tried to place the code in Tracker.autorun(), but I have no luck.

The for loop on the object doesn't trigger at all, it seems, which explains why the state variable is never updated even though the this.props.aggregate prop updates seamlessly.

vijayst

If props are changing, use the componentWillReceiveProps method.

componentWillReceiveProps(nextProps){
    let obj = nextProps.aggregate // {Gallileo: 2, Juno: 1, Mars: 1}
    let choice;
    for(let key in obj){
      if(key === nextProps.option){
        choice = obj[key];
        break;
      }
    };

    let total = 0;
    for(let key in obj){
      total += obj[key];
    }

  this.setState({choice: choice, total: total});

};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I go about making a Unix Timestamp in Milliseconds in PHP Code?

From Dev

How could I make this Meteor code reactive?

From Dev

How do I go about shortening or change this code?

From Dev

How do I get only part of my mongo document from inside my Meteor helper?

From Dev

How do I go about making multiple instances of links in changing content work?

From Dev

How do i go about making a django model field unique but only for individual users?

From Dev

How do I go on about making an image scroll inside a scrollview's specific area?

From Dev

How do I improve my Python code about heap sort?

From Dev

Making tooltips reactive in Meteor

From Dev

Making if Statement reactive in Meteor

From Dev

Meteor - how do I make this "reactive" using Deps?

From Dev

How Do I Make A Meteor Helper Non-Reactive?

From Dev

How would I go about making a RewriteRule as described?

From Dev

How do I go about setting up SSL for my API and my Web Client in a Azure Cloud Service?

From Dev

How do I go about setting up SSL for my API and my Web Client in a Azure Cloud Service?

From Dev

How do I go about setting up my Sinatra REST API on a server?

From Dev

Why is my simple SQL statement taking so long to execute and how do i go about finding the issue?

From Dev

How do I go about getting the trusted CAs to add to my certificate store

From Dev

Making Meteor reactive for html elements

From Dev

Making Meteor reactive for html elements

From Dev

WebDriver opens Microsoft Word application making the browser window go back / outfocus. How do I get my browser focus back?

From Dev

How do I save one part of my python code in a text file?

From Dev

How to go about making this type of combination?

From Dev

I can't get my Fibonacci sequence to pass this rspec test in Ruby. How do I go about it?

From Dev

How do I find my 'datasetID' for making google datastore queries?

From Dev

Go - how can i do this python code in go code?

From Dev

How do I tell Sublime Text to highlight the Go code inside my HTML?

From Dev

How do I go make my Python code more efficient and dynamic?

From Dev

How would I go about interpreting this python code?

Related Related

  1. 1

    How do I go about making a Unix Timestamp in Milliseconds in PHP Code?

  2. 2

    How could I make this Meteor code reactive?

  3. 3

    How do I go about shortening or change this code?

  4. 4

    How do I get only part of my mongo document from inside my Meteor helper?

  5. 5

    How do I go about making multiple instances of links in changing content work?

  6. 6

    How do i go about making a django model field unique but only for individual users?

  7. 7

    How do I go on about making an image scroll inside a scrollview's specific area?

  8. 8

    How do I improve my Python code about heap sort?

  9. 9

    Making tooltips reactive in Meteor

  10. 10

    Making if Statement reactive in Meteor

  11. 11

    Meteor - how do I make this "reactive" using Deps?

  12. 12

    How Do I Make A Meteor Helper Non-Reactive?

  13. 13

    How would I go about making a RewriteRule as described?

  14. 14

    How do I go about setting up SSL for my API and my Web Client in a Azure Cloud Service?

  15. 15

    How do I go about setting up SSL for my API and my Web Client in a Azure Cloud Service?

  16. 16

    How do I go about setting up my Sinatra REST API on a server?

  17. 17

    Why is my simple SQL statement taking so long to execute and how do i go about finding the issue?

  18. 18

    How do I go about getting the trusted CAs to add to my certificate store

  19. 19

    Making Meteor reactive for html elements

  20. 20

    Making Meteor reactive for html elements

  21. 21

    WebDriver opens Microsoft Word application making the browser window go back / outfocus. How do I get my browser focus back?

  22. 22

    How do I save one part of my python code in a text file?

  23. 23

    How to go about making this type of combination?

  24. 24

    I can't get my Fibonacci sequence to pass this rspec test in Ruby. How do I go about it?

  25. 25

    How do I find my 'datasetID' for making google datastore queries?

  26. 26

    Go - how can i do this python code in go code?

  27. 27

    How do I tell Sublime Text to highlight the Go code inside my HTML?

  28. 28

    How do I go make my Python code more efficient and dynamic?

  29. 29

    How would I go about interpreting this python code?

HotTag

Archive