Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React: is there a different between using curly brackets and omitting them?

With curly brackets:

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

vs without curly brackets:

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

Same question is relevant for the "ref" prop:

With curly brackets (from React's docs), accessible through this._input:

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

vs without curly brackets, accessible through this.refs.commander:

<div ref="commander"> ...

I've also noticed that everything comes out as strings. For this:

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

The props will be this (all strings):

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

So I guess only way to pass booleans and numbers is the following:

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

right?

like image 571
Guy Avatar asked Sep 13 '16 17:09

Guy


People also ask

Why we use {} In react?

The curly braces are a special syntax to let the JSX parser know that it needs to interpret the contents in between them as JavaScript instead of a string. This process is generally referred to as "interpolation".

When should curly brackets be used?

Curly brackets are rarely used in prose and have no widely accepted use in formal writing, but may be used to mark words or sentences that should be taken as a group, to avoid confusion when other types of brackets are already in use, or for a special purpose specific to the publication (such as in a dictionary).

Why use curly braces while importing in react?

if there are more than one export in the file then we need to use curly braces in the import file so that which are necessary we can import.

Is it necessary to use curly braces after the for statement?

Save this answer. Show activity on this post. If the number of statements following the for/if is single you don't have to use curly braces. But if the number of statements is more than one, then you need to use curly braces.


2 Answers

Without the curly it will be a string of diagonal-texture. With curly. React will try to evaluate it and find a string.. The end result is the same. Just that you take longer steps by telling react to evaluate it.

while the second example:

<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
             }) 
}

so yeah, in react, <div ref={} /> this will tell react to evaluate whatever inside the curly.

like image 186
vdj4y Avatar answered Nov 12 '22 00:11

vdj4y


In React JSX syntax, inside of curly braces, Javascript will be evaluated. As the documentation https://facebook.github.io/react/docs/jsx-in-depth.html#attribute-expressions says,

To use a JavaScript expression as an attribute value, wrap the expression in a pair of curly braces ({}) instead of quotes ("").

In your first example, the expression "diagonal-texture" evaluates to the same thing as the string "diagonal-texture". Not so for the expression in the second example.

like image 24
yoz Avatar answered Nov 11 '22 22:11

yoz