Upon using the setState method, you are not guaranteed your state will be updated in any particular order. I learnt this this hard way, when doing the JavaScript Calculator Project. The issue with this is when any one state object is dependant upon another state object, or the current prop object value - as seen in the example:
Essentially, when this.setState is called, this.state.counter could be equal to 1, but, due to other processes in the application, by the time the state object counter is actually updated, this.state.counter is equal to something else.
So, the arguments state and props passed to the function inside the setState are sent by a function caller(or something like that) as parameters and therefore they are the latest values?
It is very hard to understand without seeing what is happening at the background(if something is happening)
When you use this does it refer to the state on the UI? Can we not remove this and write the following code to update the current state or does it have to be done with a function? Confusing²
It refers to the component itself. So, you create a state object in the constructor (this.state), and the this in both cases refers to the same thing - the component.
But I still don’t understand how a function(handleUpdate) updates the latest and the regular way(updateCounter) doesn’t. When I use your demo, in both cases the state is updated, so I can’t see any difference apart from the functional one updates by 3 at a time and the normal way by 1.
okay, that makes sense but still hard to get my head around how using function does that. I understand that I should use function but I would understand it better if I knew how it works.
Thank you very much for your answers and the demo.
It’s like a chain, right. Learn HTML, CSS and then JavaScript, then React and Redux and then React-Redux and now TypeScript. I can’t… I am overload with too many questions already.
I think I will live with "just use the bloody function when you update the state" statement.
Also, these parenthesis and curly braces are confusing too. On the demo you sent me, you use arrow functions and there is return. I thought with arrow functions we can omit return. When I try to remove, it doesn’t work straight away as I need to arrange the parenthesis and curly braces correctly. :pullingmyhairout: