React JSX - React Tutorial for Beginners
What is JSX in ReactJS ?
- JSX denotes for JavaScript XML. It allows us to write HTML in React.
- It makes it easier to write and add HTML in React.
- JSX is a preprocessor step that adds XML syntax to JavaScript. You can definitely use React without JSX but JSX makes React a lot more elegant. Just like XML, JSX tags have a tag name, attributes, and children.
Coding JSX
- It allows us to write HTML elements in JavaScript and place them in the DOM without any appendChild() and/or createElement() methods.
- It converts HTML tags into react elements.
- To use JSX is not compulsory, but JSX makes it easier to write React applications.
- In this example, JSX allows us to write HTML directly within the JavaScript code. JSX is an extension of the JavaScript language and is translated into regular JavaScript at runtime.
Example 1
Sample Code - JSX
Output
React JSX!
Example 2
Sample Code - Without JSX
Output
Welcome to React JSX!
Expressions in JSX
- In JSX you can use expressions inside curly braces { }.
- The expression can be a React property, or variable, or some other valid JavaScript expression. It will execute the expression and return the result.
Sample Code
Output
React is 10 times better with React JSX
Inserting a Large Block of HTML
- To write HTML on several lines, put the HTML inside parentheses. In this example we can create a list with three list items.
Sample Code
Output
One Top Level Element
- HTML code should be covered in ONE top level element. So if you want to write two paragraphs, you should put them inside a parent element, like a div element.
- In this example , Wrap two paragraphs inside one DIV element. JSX will throw an error if the HTML is not correct, or if the HTML errors a parent element.
Sample Code
Output
- Alternatively, you can use a "fragment" to cover multiple lines. This will stop unreasonably adding extra nodes to the DOM.
- A fragment looks like an empty HTML tag: <></>. In this example, you can wrap two paragraphs inside a fragment.
Sample Code
Output
Elements Must be Closed
- JSX follows XML rules, and then HTML elements must be correctly closed. In the below example, close empty elements with />. JSX will throw an error if the HTML is not properly closed.
Sample Code
Output
Attribute class = className
- The class attribute is a much-used attribute in HTML, but meanwhile JSX is extracted as JavaScript, and the class keyword is a reserved word in JavaScript, you are not permitted to use it in JSX.
- You can use attribute className instead.
- JSX resolved this by using className in its place. When JSX is rendered, it interprets className attributes into class attributes.
Sample Code
Output
Hello Wikitechy
Conditions - if statements
- React supports if statements, but not inside JSX.
- If you want to use conditional statements in JSX, you must put the if statements outside of the JSX, or you should use a ternary expression instead.
Example 1
- Write if statements external of the JSX code.
Sample Code
Output
Wikitechy
Example 2
- Here you can use ternary expressions instead.
Sample Code
Output
Wikitechy