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.
state and props are snapshots of the current state and props at the time of call, as opposed to time of execution/completion.
Hope this helps
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.
Hope it helps
proxima79:
I can’t see any difference apart from the functional one updates by 3 at a time and the normal way by 1.
This is the whole point. They should both do the same - update by 3.
Without the updater function, multiple setState calls can be accidentally batched into one call.
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.
Have a good day.
Well, if you are comfortable with TypeScript, then you could have a look at the definition of setState:
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @flow
import type {
MutableSource,
MutableSourceGetSnapshotFn,
MutableSourceSubscribeFn,
ReactContext,
} from 'shared/ReactTypes';
import type {OpaqueIDType} from 'react-reconciler/src/ReactFiberHostConfig';
import invariant from 'shared/invariant';
import ReactCurrentDispatcher from './ReactCurrentDispatcher';
This file has been truncated. show original
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: