From react doc
From the documentation (https://reactjs.org/docs/handling-events.html) I could see handling of on-off button changes by arrow function like:
handleClick() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}));
}
Important thing in here (for me) is, that the new state value is derived from its previous state value. If I am right, the behaviour is the same as (without arrow function):
handleClick() {
this.setState(function (prevState){
return {isToggleOn: !prevState.isToggleOn}
});
}
or even without function at all:
handleClick() {
this.setState({isToggleOn: this.state.isToggleOn ?
false : true});
}
1) Are there any differences in these except the code-style? All approaches seems to work for me and I could not find any difference (except readability).
2) I thought, this.setState is only "setter", how the "prevState" can be passed into that function?
3) Is it mistake to write the code like this (3th approach)? I became from "old-school" and I did not develop for more than 5 years, now I am returning to dev slowly. The first one example is hardly understandable for me (because of arrow functions and anonymous function), second one is clearer a little bit but the last one is the most self-explaining to me.
Real life example
"Real learn (life)" example what I am struggling right now: As I am learning, I am trying to write my own calculator (starting with only basic functionality). Because of bad "design" (I found out it will be much better to use some array, fields, lists instead of only "variableA, variableB", but never the less..). My app structure is:
- Calculator
- ResultHeader
- Input (for result)
- Input (for printing the equation)
- CalcButton (0)
- Button
- ...
- CalcButton (X)
- Button
- ResultHeader
CalcButton contains props for its value ([0...9], x, /, +, -, C, ...) and event handlers (numberClick, operatorClick). When number button is pressed, it lifts its state to Calculator, which adds the number to valueA or valueB, depends on the state of "isFirst". operatorClick contains switch for its own functionality and if x, /, +, - was pressed, it sets isFirst on false and user can continue with second number (valueB). Now the code of this handler:
onNumberClick(e) {
function appendA() {
return {valueA: this.state.valueA + e};
}
function appendB() {
return {valueB: this.state.valueB + e};
}
this.setState(this.state.isFirst ? appendA : appendB);
/*
if(this.state.isFirst) {
this.setState({valueA: this.state.valueA + e});
} else {
this.setState({valueB: this.state.valueB + e});
}
*/
}
Commented code is basically the same as 3th example, even the uncommented variant, only with some "readability" improvements. Last 2 days I am stuck if there is any possibility to use some anonymous arrow function so I do not need to duplicate the code (one time for valueA, one time for valueB). Because of javascript and its lack of object referencing, the only solution I was able to figure was to pass the argument via string ("valueA" || "valueB") but I think it is not nice solution. The important thing in here is (as in the example at the top), the new state value is derived from its previous value (that is why I am still thinking about using arrow anonymous function).
Can somebody show me some examples how to achieve that? Is arrow function necessary in here (is it even possible to do this by arrow function?) or is this "code" just fine and I am finding problems where there are none?