CodeNewbie Community šŸŒ±

aagya11
aagya11

Posted on

Why do we need JSX in React?

Itā€™s incredibly easy to build beautiful user interfaces with React. But thereā€™s a lot that goes on beneath the simplicity of writing functional components and a layout that seemingly looks like HTML. In this article, we want to explain what JSX is and why itā€™s useful.

Introduction to JSX

Letā€™s start with basics and talk about what JSX is. In simple words, itā€™s a templating language that makes it easy to write component layouts in React. As you know, components are nothing but self-contained bits of UI, so the ability to write their layout is quite important. Of course components also need logic, and JSX makes it extremely easy to implement dynamic features inside the layout structure. Thatā€™s what makes it so useful and why React developers love JSX.

If youā€™ve ever looked at React components, first thing you probably notice is the HTML-like code inside the return statement. That code is JSX, and despite how it looks, itā€™s actually JavaScript. The fact that JSX is JavaScript disguised as HTML allows React developers to get the best of both worlds ā€“ readability and simple structure of HTML, as well as dynamic features of JavaScript.

Dynamic expressions in JSX need to be wrapped with curly braces. These indicate to JSX that the expression is dynamic, and not a static value that needs to be interpreted as-is. This distinction is quite important, as there are many static values inside JSX as well ā€“ from React element definitions and attributes to content for elements as well.

React is a purely written in JavaScript. Donā€™t let the HTML-like appearance of JSX confuse you. The template language is just an easier way to write many calls to React.createElement() API. Making calls to top-level React API is simply not readable, especially if you want to nest components, add props, and so on. So React offers JSX, a friendly syntax for structuring web applications. Setting event handlers works the same way, so you can easily handle onchange events on input elements in React.

Implementing dynamic features in JSX

As we mentioned, great thing about JSX is the ease with which you can implement dynamic features. Anything from conditional classes and styles to dynamic rendering is very easy to do with React.
The main tool to implement dynamic features and logic is the ternary operator. In case you donā€™t know, ternary operator consists of three parts ā€“ the condition to check, followed by the value to return (or an expression to run) if that condition evaluates as true, or alternative value if the condition turns out to be false. Ternary operators are incredibly useful because you can not implement multi-line JavaScript statements inside JSX. Ternary operators take up only one line and are fine to be embedded inside JSX code. Often you see className set to ternary operator, so it conditionally returns one value or another, depending on the condition.

Ternary operators are also often used to apply one style or another. This is useful when you want your application to immediately respond to changes in the state. You can store changing input value in the state and then use its value in the ternary operator, giving your web application a dynamic appearance.

Letā€™s look at an example:

In this case, there are double curly braces because one pair tells JSX to interpret its contents as dynamic JavaScript expression. Another is for opening and closing of the JavaScript object. Inside, we donā€™t need to use curly braces the third time to set the value for backgroundColor CSS property, because we have already done so before.

Note that in inline styles, the property name is different from proper CSS name. ā€˜background-colorā€™ in CSS becomes backgroundColor in React, because inline styles are stored in an object, and need to be structured as key-value pairs.

Conditional rendering in JSX

The ability to embed dynamic expressions is a very powerful feature that is absolutely necessary for building interactive web applications. For example, ternary operators also allow us to render (or not render) certain components or elements based on a condition.

Inside the JSX, create a ternary operator and set a condition. If the condition is evaluated as true, then React will invoke the component, or return null. You can even set an alternative component that you want to render in case the condition is evaluated to be false.

Event handling in JSX

Event handling is the cornerstone of building any interactive application. Thanks to JSX, itā€™s very easy to handle events in React. The names for event handlers are the same as well, with minor differences. In React, event handlers are written in camelCase. For example, onchange event handler becomes onChange in React. This is done mostly for readability, and to make it easy for other developers to distinguish between props and event handlers.

Event handlers are usually set to a function that performs a certain action when the event occurs. Letā€™s look at this example where, where clicking a button logs message to the console:

console.log(ā€œbutton clickedā€}>Click button

Important detail to remember is that event handlers are set to a function that performs the action. They are not set to the function itself. If that were the case, React would call console.log() every time it rendered the button.

Usually you can not pass arguments into the functions that handle your events, but thereā€™s a workaround that allows you to pass parameters to onClick and other event handlers. You need to pass argument to the initial function between curly braces, which passes it down to the event handler. Then you can use that value inside the event handler.

Event handlers can be quite complex and used to implement user-friendly features. For example, you can use them for scroll to position feature in React.

Summary

JSX is a foundational feature that saves developers a lot of time on building web applications with React. Its simplicity and readability, combined with ability to implement dynamic features makes it a great choice for building web apps with React. While not a requirement, I personally highly recommend to all beginner React developers to learn JSX.

Top comments (5)

Collapse
 
mrboos123d profile image
mrboos123d

We have collected many Latest Active telegram group links for you to get interesting material

Collapse
 
severussnape001 profile image
Severu Snape

Using a Japanese font generator is a fun and creative way to make your posts or stories stand out and catch the attention of your followers.

Collapse
 
mrsimon007 profile image
Mr Simon

Join Family Whatsapp Group Names where members will share interesting content

Collapse
 
goharshah0002 profile image
Chris Redfield

get the best Dolphin Names

Collapse
 
siriusblack021 profile image
chrisredfield003