I have a component in a React application that displays a common value to the user. When this value rises, I want to make noise. I realized that in a component that displays the total, would be a good place to reproduce this noise.
So, I added the componentWillReceiveProps method to the component, and in it I calculate two totals: total calculated from this.props and nextTotal calculated from nextProps .
To my surprise, even when the values ββchange and the total values ββchange, nextTotal and total always the same. Thus, the conditional that I want to shoot when the total is no good will never happen.
I wrote a simple one-component example. JSfiddle .
var Hello = React.createClass({ componentWillReceiveProps: function(nextProps) { var total = 0; this.props.vals.forEach(val => total+= val); var nextTotal = 0; nextProps.vals.forEach(val => nextTotal+= val); console.log(total, nextTotal) if (nextTotal > total) {
As you can see, every second it adds 10 to the vals array, which means that the total number moves up by 1. But if you open the console, you will see that total and nextTotal always the same, and moving up never registered.
I am clearly misunderstanding something, and if anyone can explain what my misunderstanding is, and how I should achieve what I'm going to, it would be fantastic.
source share