Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do curly braces mean in JSX (React)?

For an example, to set a style in react you could do

var css = {color: red}

and

<h1 style={css}>Hello world</h1>

Why do you need the curly braces around css in the second code snippet?

like image 326
EverRip Avatar asked May 11 '17 00:05

EverRip


People also ask

What does {} mean in JSX?

With JSX you can write expressions inside curly braces { } . The expression can be a React variable, or property, or any other valid JavaScript expression.

What does curly braces mean in JSX?

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. You need them when you want to use a JavaScript expression like a variable or a reference inside JSX.

What does curly braces mean in programming?

In programming, curly braces (the { and } characters) are used in a variety of ways. In C/C++, they are used to signify the start and end of a series of statements. In the following expression, everything between the { and } are executed if the variable mouseDOWNinText is true. See event loop.

Why curly braces in import React?

If there is any default export in the file, there isn't any need to use the curly braces in the import statement. 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.


4 Answers

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.

You need them when you want to use a JavaScript expression like a variable or a reference inside JSX. Because if you use the standard double quote syntax like so:

var css = { color: red }

<h1 style="css">Hello world</h1>

JSX doesn't know you meant to use the variable css in the style attribute instead of the string. And by placing the curly braces around the variable css, you are telling the parser "take the contents of the variable css and put them here". (Technically its evaluating the content)

This process is generally referred to as "interpolation".

like image 171
Daniel Sandiego Avatar answered Oct 11 '22 07:10

Daniel Sandiego


If you don't use the variable css, the JSX could look like this:

<h1 style={ {color: 'red'} }>Hello world</h1>

I guess you are confused about the double curly braces.

so you know that the curly braces in JSX means process the inner value in JavaScript, so the outer braces is used exactly for this purpose.

But the style property accepts an object. And an object also needs another pair of curly braces to wrap it up. That's the purpose for the inner ones.

like image 33
Chang Avatar answered Oct 11 '22 08:10

Chang


You put curly braces when you want to use the value of a variable inside "html" (so inside the render part). It's just a way of telling the app to take the value of the variable and put it there, as opposed to a word.

like image 4
adinutzyc21 Avatar answered Oct 11 '22 07:10

adinutzyc21


The outer curly braces tell the JSX parser that the syntax should be interpreted as javascript. The inner braces are used because the style variable accepts an object.

let's break this down:

<h1 style={interpret javascritp {interpret the object} }> hello </h1>
like image 4
Melanie H. Avatar answered Oct 11 '22 06:10

Melanie H.