Hello React Developers,
Today I will explain the basics of JSX in React.
JSX is used to add HTML code inside JavaScript. Check out the Beginner’s Guide of ReactJS.
In this guide, we will cover an introduction to React JSX, and why we use JSX in React with an example.
Contents
Take the following variable declaration
const element = <h1>Hello, world!</h1>;
This syntax is neither a string nor HTML.
It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript.
JSX produces React “elements”.
React embraces the fact that rendering logic is inherently coupled with other UI logic: how events are handled, how the state changes over time, and how the data is prepared for display.
Instead of artificially separating technologies by putting markup and logic in separate files, React separates concerns with loosely coupled units called “components” that contain both, we will return to components in a different section, but if you’re not yet comfortable putting markup in JS, this talk might convince you otherwise.
React doesn’t require using JSX, but most people find it helpful as a visual aid when working with UI inside the JavaScript code. It also allows React to show more useful error and warning messages.
In the example below, we declare a variable called name and then use it inside JSX by wrapping it in curly braces:
Hello, {name} lang:default decode:true " >const name = 'John Deo'; const element = <h1>Hello, {name}</h1>;
In the example below, we embed the result of calling a JavaScript function,
showName(user), into an <h1> element. function formatName(user) { return user.firstName + ' ' + user.lastName; } const user = { firstName: 'John', lastName: 'Deo' }; const element = ( <h1> Hello, {formatName(user)}! </h1> );
After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects.
This means that you can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions:
function getGreeting(user) { if (user) { return <h1>Hello, {formatName(user)}!</h1>; } return <h1>Hello, Stranger.</h1>; }
We use quotes to specify string literals as attributes
const element = <a href="https://www.reactjs.org"> link </a>;
We can also use curly braces to embed a JavaScript expression in an attribute
const element = <img src={user.avatarUrl}></img>;
This was all about JSX in ReactJS. If you have any queries or want to know more about React JSX, you can freely comment below. Share the React JSX tutorial with your ReactJs friends and help them broaden their skills.
Happy Reading!
Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…
In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…
October was an exciting month for MageComp! From significant updates across our Magento 2 extension…
In modern web development, seamless navigation and state management are crucial for delivering a smooth…
Magento Open Source 2.4.8 beta version released on October 8, 2024. The latest release of…
Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…