Skip to content
  • Homepage
  • HTML
  • CSS
  • Symfony
  • PHP
  • How to
  • Contact
  • Donate

Teach Developer

Articles, Guides & Tips

Important most things you should know about JSX

Home  »  JavaScript • React   »   Important most things you should know about JSX
Posted on July 31, 2022July 31, 2022 No Comments on Important most things you should know about JSX
863

What JSX is

JSX is a syntax extension for JavaScript. Basically, it extends JavaScript so that constructs like HTML/XML can be used with JavaScript. It allows developers to use HTML syntax to compose JavaScript components. This makes it possible to have a clear and familiar syntax for defining DOM tree structures with attributes.

Although it looks a lot like HTML, it’s actually a JS file. Since it’s an extension to JavaScript, your browser won’t understand it unless it’s compiled into plain JavaScript. So it would help if you used a JSX compiler like Babel for that.

JSX is a JavaScript Expression

JSX expressions are also JavaScript expressions. When compiled, it actually becomes a regular JavaScript object. For example, the following code:

const hello = <h1 className = "greet"> Hello World </h1>

will be compiled to

const hello = React.createElement {
	type: "h1",
	props: {
	className: "greet",  
	children: "Hello World" 
	}
}

Notice that className was used instead of class. I’ll explain that shortly.

Because they are compiled for objects, JSX can be used wherever regular JavaScript expressions can be used. They can be saved in variables as we did above, used in loops, passed as arguments to functions, returned from functions, stored in arrays and objects… anywhere JavaScript expressions can be used.

Nesting in JSX

Just like HTML, you can nest several lines of JSX within one element. To do that, you need to nest all of them in brackets () like the example below.

(
	<div>
		<h1> Hello World!!! </h1>
		<p> I am a human </p>
	</div>
)       

One outermost element, please

A JSX expression must have only one outer element. Therefore, while this will work

const headings = (
	<div id = "outermost-element">
		<h1>I am a heading </h1>
		<h2>I am also a heading</h1> 
	</div>
);

this will not work

const headings = (
	<h1>I am a heading </h1>
	<h2>I am also a heading</h1>
);

For the love of camelCase

Since JSX is closer to JavaScript than HTML, JSX properties use camelCase naming convention instead of HTML attribute names. That was why point 2, className was used and tabIndex is used instead of tabindex. This is also true of event listeners in JSX. While lowercase is used for event listeners in HTML, camelCase is used in JSX.

This will work in JSX

<button onClick = {handleClick}>Click Me</button>

This will not work in JSX

<button onclick = {handleClick}>Click Me</button>

JavaScript in JSX

You can embed JavaScript in JSX, but to do this, you need to make use of curly braces. For instance, if you want to add `2+3` in a JSX expression, you’ll do this like this.

<p> 2+3 = {2+3} </p>

will result to

2+3 = 5

Displaying JSX

For JSX to appear on the screen of a browser, it has to be rendered. ReactDOM.render() is the most common way to render JSX. ReactDOM is a JavaScript library that contains several methods that deal with the DOM in some way.

ReactDOM.render() takes the JSX expression, creates corresponding tree nodes, and adds it to the DOM. That way your JSX expression appears on the screen. To get your JSX to appear onscreen, you pass two arguments to the ReactDOM.render(). The first argument is the JSX you want to render, which could be the raw JSX itself or the name of a variable that contains JSX. The second argument is the target element in the HTML file to which you want the JSX to be appended to.

To understand better, look at the following example.

const names = (
	<ul>
		<li>Teach</li>
		<li>Developer</li>
	</ul>
);

ReactDOM.render(names , document.getElementById("app"));

Try it on Codepen

Self Closing Tags in JSX

For HTML elements that have self-closing tags such as <img>, <hr>, <input> and br, the forward slash before the closing angle bracket should be included in JSX. While this is optional in HTML, it is compulsory in JSX.

while this is good in JSX

<hr />

this is not good in JSX

<hr>

Don’t forget the Keys

When making a list in JSX, your list should include a JSX attribute called Keys. Keys are similar to id and they are used internally by React to keep track of the list items. Without this, React might scramble the order of the list. So if the order of your list is important, the key attribute is important. Below is how you use keys.

const toDO = (
	<ul>
		<li key = "todo-1"> Design </li>
		<li key = "todo-2"> Develop </li>
		<li key = "todo-3"> Be Awesome </li>
	</ul>
);
JavaScript, React

Post navigation

Previous Post: ReactJs Properties
Next Post: How to Create Live Search in HTML table with jQuery

Related Posts

  • Difference between var, let, and const in JavaScript
  • How to Deploy or Host your ReactJS App in cPanel
  • How to trigger a button click from another button click event with JavaScript?
  • How to use the Local Storage in Javascript
  • How to Reverse A String with JavaScript
  • How to use setTimeout and setInterval methods in JavaScript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Codeigniter (3)
  • CSS (11)
  • eCommerce (1)
  • Framework (1)
  • Git (3)
  • How to (43)
  • HTML (5)
  • JavaScript (15)
  • Jquery (7)
  • Laravel (1)
  • Linux (4)
  • Magento-2 (1)
  • Node js (4)
  • Others (2)
  • PHP (11)
  • React (13)
  • Server (1)
  • SSH (3)
  • Symfony (6)
  • Tips (16)
  • Top Tutorials (10)
  • Ubuntu (3)
  • Vue (1)
  • Wordpress (7)

Latest Posts

  • What is SSH in Linux?
  • How to Delete Files in Ubuntu Command Line
  • How to Deploy a React application on a cPanel
  • How to use events listeners and Event Subscriber in Symfony
  • How to Convert PHP CSV to JSON

WEEKLY TAGS

AJAX (1) Codeigniter (1) Javascript (11) JQuery (1) PHP (16) Programming (1) React (3) Symfony (1)

Random Post

How to Search Recently Modified Files in Linux
How to make the div move up and down when scrolling the page with CSS?
How to use setTimeout and setInterval methods in JavaScript
How do sum values from an array of key-value pairs in JavaScript?
How to add two-factor authentication (2FA) to WordPress using the Google Authenticator plugin.

Quick Navigation

  • About
  • Contact
  • Privacy Policy

© Teach Developer 2021. All rights reserved