javascript - ReactJS - How a property can be setted by an argument following a comma?
问题描述
I work currently on a specific section ReactJS documentation - here the corresponding link : in https://reactjs.org/docs/lifting-state-up.html or on codepen : https://codepen.io/gaearon/pen/WZpxpz?editors=0010#0 .
There is something in the code which interrogate me : How is possible that a value be updated by an argument passed in a function following a coma. Here the example to be more understandable :
handleCelsiusChange(temperature) {
this.setState({scale: 'c', temperature});
}
corresponding to :
which handle :
class Calculator extends React.Component {
constructor(props) {
super(props);
this.handleCelsiusChange = this.handleCelsiusChange.bind(this);
this.handleFahrenheitChange = this.handleFahrenheitChange.bind(this);
this.state = {temperature: '', scale: 'c'};
}
How the temperature argument can handle the setting of temperature property, if someone can explain me it would be great.
Kind regard, J.Doe
解决方案
This :
handleCelsiusChange(temperature) {
this.setState({scale: 'c', temperature});
}
is equivalent to this : (notice the temperature
property)
handleCelsiusChange(temperature) {
this.setState({
scale: 'c',
temperature: temperature
});
}
Which is equivalent to this :
handleCelsiusChange(newValue) {
this.setState({
scale: 'c',
temperature: newValue
});
}
The first syntax is the new shorthand form introduced in ES6
推荐阅读
- c# - OnEntry 方法不起作用,当使用 postsharp 进行 AOP 时
- java - 在 Spring 中管理(检查和恢复)自定义背景例程的标准方法是什么?
- java - Java 运算符 && 与 || 的求值顺序
- javascript - 如何在按下按钮时在屏幕中间打开一个组件?
- html - 通过 svg 线连接部分(响应式)并在滚动上绘制
- javascript - 内容安全策略在 iPhone 上不起作用
- api - 颤动的http post请求显示状态码500,尽管它在邮递员中工作
- java - 将时区字符串日期解析为 Java 的 Instant
- node.js - 我如何将(例如!lang en)保存到 json 文件中,以便机器人使用该 lang 的差异命令
- rest - 如何使用 apollo 客户端进行休息发布请求?