JSX is an HTML-like syntax from React that extends ECMAScript. JSX allows you to write HTML code mixed with JavaScript. Get started with JSX.
JSX is an XML/HTML-like syntax from React that extends ECMAScript. JSX allows you to write HTML code mixed with JavaScript. Take a look at the following example:
Each JSX code is converted into the React.createElement function, which the web browser knows. Take a look at the converted code I showed you in a previous snippet:
The react createElement function creates and returns a new React element. Here is the function signature:
The type argument can be an HTML element or React element (or even React fragment). The second argument is the props object which contains HTML tag attributes or props of a React element. The last argument is children, and so this is another jsx element transformed to React createElement function.
In the above code, you can see there are nested elements. All jsx tags are transported to react createElement functions, and regular javaScript can understand them.
So if you ask what is jsx in React, what is jsx meaning, I may answer you that is a special syntax that allows mixing plain JavaScript and HTML elements to speed up development. Then you can ask…
Is JSX necessary for React?
It’s not so if you don’t like JSX syntax, or you want to write JavaScript code without configuring babel to transpioling JSX syntax to actual javascript code, you can write React apps without JSX.
Pro-tip for you then: if you want to avoid writing React.createElement too much, you can try something like this:
React JSX Explained with Examples
React JSX is React’s core concept. If you have a good understanding of it properly, then you can write React code efficiently. Let’s see common use cases of JSX in React.
How do I add JavaScript code in JSX?
To add JSX-style code, you need to use curly brackets like this:
JSX doesn’t allow to render objects directly as a JSX tag, so this react code is not valid, and the error occurs when using JSX Expression when rendering an object.
But you can write arrays in JSX Expressions because arrays are converted to strings while rendering. So this react’ component is valid:
Attributes
JSX allows you to use native HTML attributes and your own custom attributes as well. For native HTML attributes, JSX uses camel case convention instead of the normal HTML naming convention. Besides, please note that, for example, a class word is a reserved keyword and for either. For those keywords, JSX uses a className for a class and a htmlFor for a reserved keyword (for).
Custom HTML attributes in JSX
For custom HTML attributes, you must use a data prefix. It looks pretty the same as regular HTML.
As you can see in that react component on
HTML tag, I used the data-isCustom attribute and curly braces to pass value to the attribute. Moreover, you can pass values to attributes of HTML tags using string literals like this:
JavaScript Expressions
JavaScript expression may be used inside JSX. What is not surprising, I suppose, when you want to use a JavaScript expression, you need to wrap it by curly braces {}
If statements
You cannot use if-else statements in JSX. Instead, you can use conditional or JavaScript ternary expressions. Take a look at the simple React component with JSX if statement:
For if/else you can use something like this:
I prefer to explode JavaScript expressions to const (and sometimes memorize them using React useMemo hook because of performance reasons). Take a look at the JSX code below:
Looping
When you want to render a JavaScript array using JSX, you can use something like this:
Spread operator
The spread operator is not supported as a child jsx expression, so you are not able to put something like this to a react component:
Comments in JSX code
If you want to insert a comment to the react component, you need to wrap its using curly braces.
JSX Styling
React encourages to use of inline styles. You can use JSX expressions to pass value to the style attribute to set inline styles. React will also automatically append px following the number of the specified property.
Note: inline styling is not the only way to style react elements. There are a few other approaches, but this is the topic for another article.
JSX: Conclusion
When you work on the React application, you can use JSX to put markup into javascript code. That means that you have HTML tags and JavaScript expressions in the same file.
Each JSX tag is transformed to React createElement javascript function. Moreover, If you don’t want to use JSX for any reason, you can write react code without JSX.
In my opinion, JSX has many advantages, and definitely, it’s a friend of any React developer. I love it the most in JSX because it looks like JavaScript with more power (It’s fair to say that JSX is built on top of JavaScript). I like that approach more than something you can find in Vue.js or Angular, where you need to learn specific framework functions and syntax.