sof*_*ode 7 javascript jsx ecmascript-6 reactjs
我很难理解以下 ES6 语法。我阅读了很多文档,似乎这里发生了不止一个变化:
const renderInput = props =>
<div>
<label>{props.placeholder}</label>
</div>
Run Code Online (Sandbox Code Playgroud)
以上是否等同于:
const renderInput = function renderInput(props) {
return <div>
<label>{props.placeholder}</label>
</div>
}
Run Code Online (Sandbox Code Playgroud)
?
对,那是正确的。当您只有一个表达式,并且它是您希望从函数返回的表达式时,您可以省略大括号。
<div><label>{props.placeholder}</label></div>事实上,since是一个单一的表达式(它被转译成React.createElement(......)类似的东西),并且你希望从 中返回它renderInput,这确实是你使用箭头函数的无括号版本的方式。
如果您希望使用变量或进行其他一些计算(条件、for 循环等),则无法省略括号。