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)
We have collected many Latest Active telegram group links for you to get interesting material
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.
Join Family Whatsapp Group Names where members will share interesting content
get the best Dolphin Names
Join the Latest USA Girls WhatsApp Group Links