In React, to handle variable changes, we do:
handleChangeRound = (round) => {
this.setState({round: round});
}
updateLoading = (isLoading) => {
this.setState({isLoading: isLoading});
}
Is there a way to write
this.setState({round: round});
this.setState({isLoading: isLoading});
as
this.updateState(round);
this.updateState(isLoading);
given the variables round
and isLoading
exist in the state and correspond to the variable name to avoid the redundant variable: variable
?
PS: Inspired by:
console.log({x: x, y: y});
// output:
// {x: 10, y:20}
can be written as
console.log({x,y});
// output:
// {x: 10, y:20}
You can write it as
this.setState({round});
this.setState({isLoading});
by using the Object Property Shorthand from ECMAScript 6 / ES2015. Basically the idea behind this is that you can omit the property key since the variable declaration has the key.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments