2016-09-13 23 views
0

用两个大括号:反应:使用大括号和忽略它们有什么不同吗?

<div theBestProp={"diagonal-texture"}> ... 

VS无大括号:

<div theBestProp="diagonal-texture"> ... 

同样的问题是相关的 “裁判” 道具:

用两个大括号(来自阵营的文档),访问通过this._input

<div ref={(c) => this._input = c} ... 

VS没有大括号,访问通过this.refs.commander

<div ref="commander"> ... 

我也注意到,一切都出来为字符串。对于这一点:

<PriceOption id="1" yes="true" price="free" audience="for individuals" plan="Starter" /> 

道具将是(所有字符串):

{ 
     "id": "1", 
     "yes": "true", 
     "price": "free", 
     "audience": "for individuals", 
     "plan": "Starter" 
    } 

所以我想只能通过布尔值和数字的方式如下:

<PriceOption id={1} yes={true} price="free" audience="for individuals" plan="Starter" /> 

吧?

+0

嗯,是的..它可能有助于认为PriceOption = {id:1,price:“free”},它是PriceOption是一个具有属性的对象。 (这被称为道具的原因) – vdj4y

回答

3

没有花这将是对角纹理的字符串。 卷曲。 React会尝试评估它并找到一个字符串。 最终结果是一样的。只需通过告诉反应来评估它就可以采取更长的步骤。

,而第二个例子:

<div ref={(c) => this._input = c} 
// react will run the function inside ref, 
// the arrow function always return something, that's js not react 
// this is javascript ES6, not react, 

// the function above is the same as: 
<div ref= { (c) => {     // arrow function returns a closure 
       return this._input = c // the closure is returning this._input 
      }) 
} 

所以是的,在反应过来,这<div ref={} />将告诉反应来评价无论花里面。

1

在阵营JSX语法,大括号内,使用Javascript进行评价。如文档https://facebook.github.io/react/docs/jsx-in-depth.html#attribute-expressions所述,

要将JavaScript表达式用作属性值,请将表达式换成一对大括号({})而不是引号(“”)。

在第一个示例中,表达式"diagonal-texture"的计算结果与字符串"diagonal-texture"的计算结果相同。第二个例子中的表达式并非如此。

相关问题