Building with React.js from Lagos: Part 2 — Learn JSX

Part 2 of a new series of learning React.js. This section, we’ll be looking at Advanced JSX.

Jide Lambo.
Hackmamba
4 min readAug 28, 2017

--

Adding className attributes in JSX

Grammar in React JSX is mostly the same with HTML, but there are slight differences to watch out for.

In HTML, we commonly use class as an attribute name:

In JSX, you use className instead of the word class!

Since JSX is closer to JavaScript than HTML, React DOM uses camelCase property naming convention instead of HTML attribute names. When JSX is rendered, JSX className attributes are automatically rendered as class attributes.

Self-Closing Tag and Curly Braces in JSX

When you write a self-closing tag in HTML i.e (<img>, <input>, <br>), it is optional to include a forward-slash immediately before the final angle-bracket:

But! In JSX, you have to include the slash else you will raise an error:

Any code in between a JSX element tag will be read as JSX, not as regular Javascript! JSX doesn’t add numbers — it reads them as text, just like HTML. You need to write the code by wrapping them in curly braces. This way you are saying, “Even though my code is located in between JSX tags, treat me like ordinary Javascript and not like JSX.”

Variables and Variable Attributes in JSX

You can access variables while inside of a JSX expression, even if those variables were declared on the outside.

Here’s an example of how Variable Attributes work:

Event Listeners and Conditional Statements in JSX

JSX elements can use event listeners, just like HTML elements can. You can use an event listener by giving the JSX element a special attribute. Here’s an example:

An event listener attribute’s value should be a function. The above example only works if myFunc were a valid function that had been defined elsewhere:

Now that you’ve learned how to use curly braces to inject Javascript into a JSX expression, there’s a rule that you need to know: you can not inject an if statement into a JSX expression.

The reason why the code above wont work is the way JSX compiles. You can learn more about that here but that shouldn’t set us back. If the above example doesn’t work, If you can’t inject an if statement into JSX, how can you write a conditional statement? Lets follow the example below:

The above example works because the if and else are not included in between JSX tags. One option is to write an if statement, and not inject it into JSX.

There’s another way to write conditionals in JSX: the ternary operator. Here is how you can use the ternary operator in a JSX expression:

In the above example, if age is greater than or equal to drinkingAge, then textTernary will be <h1>Serve Beer</h1>. Otherwise, textTernary will equal <h1>Serve water or candy bar</h1>.

You can do more stuffs in JSX like use the && Conditionals, map and keys…just like we use them in ordinary Javascript. Hopefully, you can put this series into good use and create an awesome React app.

--

--