{"id":4428,"date":"2022-03-16T11:13:00","date_gmt":"2022-03-16T16:13:00","guid":{"rendered":"http:\/\/upmostlymulti.onpressidium.com\/?p=4428"},"modified":"2022-09-19T19:42:48","modified_gmt":"2022-09-20T00:42:48","slug":"what-is-jsx-in-react","status":"publish","type":"post","link":"http:\/\/upmostly.com\/tutorials\/what-is-jsx-in-react","title":{"rendered":"What is JSX in React?"},"content":{"rendered":"\n<p>One of the first concepts you will stumble upon when working with React is <strong>JSX<\/strong>. It stands for JavaScript XML and is a syntax extension to JavaScript that is used to help us define our UIs.<\/p>\n\n\n\n<p>JSX might seem similar to other templating systems, but keep in mind that whereas in other templating systems we have an HTML-first approach, here we have the full power of JavaScript at our hands!<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>You can imagine JSX as being a combination of HTML and JavaScript, as that&#8217;s what it basically is.<\/p><\/blockquote>\n\n\n\n<p>A snippet of JSX would look something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">const element = &lt;div&gt;Hello, JSX&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Why JSX?<\/h2>\n\n\n\n<p>React fully embraces the fact that rendering logic is tightly coupled with other UI logic. That&#8217;s reflected in caring about how events are handled, how the state changes over time, and how the data is being prepared for being displayed.<\/p>\n\n\n\n<p>Instead of artificially separating technologies by putting markup and logic in separate files (creating separate <strong>.css<\/strong>, <strong>.html<\/strong>, <strong>.js<\/strong> files), React separates concerns with loosely coupled units called <strong>components<\/strong> that contain both.<\/p>\n\n\n\n<p>However, this approach is often disregarded when working with larger-scale applications, as it creates clunky and hard-to-read component files.<\/p>\n\n\n\n<p>We do, however, have access to technologies that help us modularize our codebase by splitting the stylesheets into separate components such as <strong><em><a href=\"https:\/\/styled-components.com\/\" data-type=\"URL\" data-id=\"https:\/\/styled-components.com\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">styled-components<\/a><\/em><\/strong> and <strong><em><a href=\"https:\/\/www.npmjs.com\/package\/node-sass\" data-type=\"URL\" data-id=\"https:\/\/www.npmjs.com\/package\/node-sass\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">node-sass<\/a><\/em><\/strong> (for SASS\/SCSS modules) which helps us combat this approach.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Working with JavaScript Expressions in JSX<\/strong><\/h2>\n\n\n\n<p>In order to work with JavaScript within JSX, we&#8217;ll have to put the expressions within curly braces so React knows it&#8217;s not part of the markup.<\/p>\n\n\n\n<p>Let&#8217;s say we wish to format the name of a user to be the combination of their first name and their last name separated by a space. We would end up with the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">function formatName(user) {\n  return user.firstName + ' ' + user.lastName;\n}\n\nconst user = {\n  firstName: 'Harper',\n  lastName: 'Perez'\n};\n\nconst element = (\n  &lt;h1&gt;\n    Hello, {formatName(user)}!\n  &lt;\/h1&gt;\n);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>JSX is an Expression in itself<\/strong><\/h2>\n\n\n\n<p>After compilation, JSX expressions become regular Javascript function calls and they are evaluated as actual Javascript objects.<\/p>\n\n\n\n<p>This means that you can use JSX inside of\u00a0<code>if<\/code>\u00a0statements and\u00a0<code>for<\/code>\u00a0loops, assign it to variables, accept them as arguments, and return it from functions as in the example below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">function getGreeting(user) {\n  if (user) {\n    return &lt;h1&gt;Hello, {formatName(user)}!&lt;\/h1&gt;;\n  }\n  return &lt;h1&gt;Hello, Stranger.&lt;\/h1&gt;;\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-background-color has-background is-layout-flow wp-block-quote-is-layout-flow\"><p><span style=\"font-size: revert;color: initial;, sans-serif\">Since <\/span><strong style=\"font-size: revert;color: initial;, sans-serif\">JSX<\/strong><span style=\"font-size: revert;color: initial;, sans-serif\"> is closer to Javascript than to HTML, React DOM uses <\/span><strong style=\"font-size: revert;color: initial;, sans-serif\">camelCase<\/strong><span style=\"font-size: revert;color: initial;, sans-serif\"> property naming convention instead of HTML attribute names.<\/span><\/p><p>For example, the <strong><em>class<\/em><\/strong> keyword becomes <strong><em>className<\/em><\/strong>, and, <strong><em>onclick<\/em><\/strong>, for example, becomes <strong><em>onClick<\/em><\/strong>.<\/p><\/blockquote>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Specifying Children with JSX:<\/strong><\/h2>\n\n\n\n<p>If a tag is empty (doesn&#8217;t have children), we can close it immediately with <strong><code>\/&gt;<\/code><\/strong> like XML or HTML:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">const element = &lt;img src={user.avatarUrl} \/&gt;;<\/code><\/pre>\n\n\n\n<p>JSX tags may contain children the same way HTML elements may:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">const element = (\n  &lt;div&gt;\n    &lt;h1&gt;Hello!&lt;\/h1&gt;\n    &lt;h2&gt;Good to see you here.&lt;\/h2&gt;\n  &lt;\/div&gt;\n);<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>JSX consists of actual JavaScript objects<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/babeljs.io\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/babeljs.io\/\" rel=\"noreferrer noopener nofollow\">Babel<\/a> compiles JSX down to <code>React.createElement()<\/code> calls.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Note that React can be used without JSX, although you won&#8217;t see much of it in production-grade applications. However, if you&#8217;re curious about how you can do that, you can check <strong><a href=\"https:\/\/upmostly.com\/tutorials\/can-i-use-react-without-jsx\" data-type=\"URL\" data-id=\"https:\/\/upmostly.com\/tutorials\/can-i-use-react-without-jsx\" target=\"_blank\" rel=\"noreferrer noopener\">this<\/a><\/strong> article, as it goes a bit more in-depth about how you can do that.<\/p><\/blockquote>\n\n\n\n<p>Anyway, you can notice in the example below the same element written using JSX and without using it:<\/p>\n\n\n\n<pre title=\"Element written using JSX\" class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">const element = (\n  &lt;h1 className=\"greeting\"&gt;\n    Hello, world!\n  &lt;\/h1&gt;\n);<\/code><\/pre>\n\n\n\n<pre title=\"Element written without JSX\" class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">const element = React.createElement(\n  'h1',\n  {className: 'greeting'},\n  'Hello, world!'\n);<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><code>React.createElement()<\/code> performs a few checks to help us write bug-free code, but essentially it creates an object like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">\/\/ Note: this structure is simplified\nconst element = {\n  type: 'h1',\n  props: {\n    className: 'greeting',\n    children: 'Hello, world!'\n  }\n};<\/code><\/pre>\n\n\n\n<p>These objects are called <strong>React elements<\/strong>. You can think of them as descriptions of what you want to see on the screen. React reads these objects and uses them to construct the DOM and keep it up to date.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>Hopefully, by the end of this article, you&#8217;ve gotten a better understanding of what JSX is, and what it is used for, and got a glimpse of how it&#8217;s being used under the hood by React to deliver those nicely constructed UIs.<\/p>\n\n\n\n<p>If you enjoyed this article be sure to stay tuned on <strong><em><a href=\"https:\/\/upmostly.com\/\" data-type=\"URL\" data-id=\"https:\/\/upmostly.com\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Upmostly<\/a><\/em><\/strong>, as we&#8217;re publishing plenty of articles on the subject of React that might help you become the next industry leader!<\/p>\n\n\n\n<p>If you feel I&#8217;ve left out anything important, or if you want to leave feedback of any kind, feel free to leave a comment down below!<\/p>\n\n\n\n<p>See you on the next one. Cheers!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSX is short for Javascript XML. Basically, JSX allows us to write HTML in React.<\/p>\n","protected":false},"author":126,"featured_media":5258,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[33,4],"class_list":["post-4428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-beginner-react-tutorials","tag-react"],"acf":[],"_links":{"self":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/4428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/users\/126"}],"replies":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/comments?post=4428"}],"version-history":[{"count":4,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/4428\/revisions"}],"predecessor-version":[{"id":10920,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/4428\/revisions\/10920"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/media\/5258"}],"wp:attachment":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/media?parent=4428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/categories?post=4428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/tags?post=4428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}