{"componentChunkName":"component---src-templates-blog-js","path":"/blog/2020/09/22/introducing-the-new-jsx-transform.html","result":{"data":{"markdownRemark":{"html":"<p>Although React 17 <a href=\"/blog/2020/08/10/react-v17-rc.html\">doesn’t contain new features</a>, it will provide support for a new version of the JSX transform. In this post, we will describe what it is and how to try it.</p>\n<h2 id=\"whats-a-jsx-transform\"><a href=\"#whats-a-jsx-transform\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What’s a JSX Transform? </h2>\n<p>Browsers don’t understand JSX out of the box, so most React users rely on a compiler like Babel or TypeScript to <strong>transform JSX code into regular JavaScript</strong>. Many preconfigured toolkits like Create React App or Next.js also include a JSX transform under the hood.</p>\n<p>Together with the React 17 release, we’ve wanted to make a few improvements to the JSX transform, but we didn’t want to break existing setups. This is why we <a href=\"https://babeljs.io/blog/2020/03/16/7.9.0#a-new-jsx-transform-11154httpsgithubcombabelbabelpull11154\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">worked with Babel</a> to <strong>offer a new, rewritten version of the JSX transform</strong> for people who would like to upgrade.</p>\n<p>Upgrading to the new transform is completely optional, but it has a few benefits:</p>\n<ul>\n<li>With the new transform, you can <strong>use JSX without importing React</strong>.</li>\n<li>Depending on your setup, its compiled output may <strong>slightly improve the bundle size</strong>.</li>\n<li>It will enable future improvements that <strong>reduce the number of concepts</strong> you need to learn React.</li>\n</ul>\n<p><strong>This upgrade will not change the JSX syntax and is not required.</strong> The old JSX transform will keep working as usual, and there are no plans to remove the support for it.</p>\n<p><a href=\"/blog/2020/08/10/react-v17-rc.html\">React 17 RC</a> already includes support for the new transform, so go give it a try! To make it easier to adopt, after React 17 is released, we also plan to backport its support to React 16.x, React 15.x, and React 0.14.x. You can find the upgrade instructions for different tools <a href=\"#how-to-upgrade-to-the-new-jsx-transform\">below</a>.</p>\n<p>Now let’s take a closer look at the differences between the old and the new transform.</p>\n<h2 id=\"whats-different-in-the-new-transform\"><a href=\"#whats-different-in-the-new-transform\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What’s Different in the New Transform? </h2>\n<p>When you use JSX, the compiler transforms it into React function calls that the browser can understand. <strong>The old JSX transform</strong> turned JSX into <code class=\"gatsby-code-text\">React.createElement(...)</code> calls.</p>\n<p>For example, let’s say your source code looks like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">import</span> React <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">App</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">Hello World</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Under the hood, the old JSX transform turns it into regular JavaScript:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">import</span> React <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">App</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> React<span class=\"token punctuation\">.</span><span class=\"token function\">createElement</span><span class=\"token punctuation\">(</span><span class=\"token string\">'h1'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">null</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'Hello world'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<blockquote>\n<p>Note</p>\n<p><strong>Your source code doesn’t need to change in any way.</strong> We’re describing how the JSX transform turns your JSX source code into the JavaScript code a browser can understand.</p>\n</blockquote>\n<p>However, this is not perfect:</p>\n<ul>\n<li>Because JSX compiled into <code class=\"gatsby-code-text\">React.createElement</code>, <code class=\"gatsby-code-text\">React</code> needed to be in scope if you use JSX.</li>\n<li>There are some <a href=\"https://github.com/reactjs/rfcs/blob/createlement-rfc/text/0000-create-element-changes.md#motivation\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">performance improvements and simplifications</a> that <code class=\"gatsby-code-text\">React.createElement</code> does not allow.</li>\n</ul>\n<p>To solve these issues, React 17 introduces two new entry points to the React package that are intended to only be used by compilers like Babel and TypeScript. Instead of transforming JSX to <code class=\"gatsby-code-text\">React.createElement</code>, <strong>the new JSX transform</strong> automatically imports special functions from those new entry points in the React package and calls them.</p>\n<p>Let’s say that your source code looks like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">function</span> <span class=\"token function\">App</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">Hello World</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>This is what the new JSX transform compiles it to:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token comment\">// Inserted by a compiler (don't import it yourself!)</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span>jsx <span class=\"token keyword\">as</span> _jsx<span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react/jsx-runtime'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">App</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token function\">_jsx</span><span class=\"token punctuation\">(</span><span class=\"token string\">'h1'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span> children<span class=\"token operator\">:</span> <span class=\"token string\">'Hello world'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Note how our original code <strong>did not need to import React</strong> to use JSX anymore! (But we would still need to import React in order to use Hooks or other exports that React provides.)</p>\n<p><strong>This change is fully compatible with all of the existing JSX code</strong>, so you won’t have to change your components. If you’re curious, you can check out the <a href=\"https://github.com/reactjs/rfcs/blob/createlement-rfc/text/0000-create-element-changes.md#detailed-design\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">technical RFC</a> for more details about how the new transform works.</p>\n<blockquote>\n<p>Note</p>\n<p>The functions inside <code class=\"gatsby-code-text\">react/jsx-runtime</code> and <code class=\"gatsby-code-text\">react/jsx-dev-runtime</code> must only be used by the compiler transform. If you need to manually create elements in your code, you should keep using <code class=\"gatsby-code-text\">React.createElement</code>. It will continue to work and is not going away.</p>\n</blockquote>\n<h2 id=\"how-to-upgrade-to-the-new-jsx-transform\"><a href=\"#how-to-upgrade-to-the-new-jsx-transform\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to Upgrade to the New JSX Transform </h2>\n<p>If you aren’t ready to upgrade to the new JSX transform or if you are using JSX for another library, don’t worry. The old transform will not be removed and will continue to be supported.</p>\n<p>If you want to upgrade, you will need two things:</p>\n<ul>\n<li><strong>A version of React that supports the new transform</strong> (currently, only <a href=\"/blog/2020/08/10/react-v17-rc.html\">React 17 RC</a> supports it, but after React 17.0 has been released, we plan to make additional compatible releases for 0.14.x, 15.x, and 16.x).</li>\n<li><strong>A compatible compiler</strong> (see instructions for different tools below).</li>\n</ul>\n<p>Since the new JSX transform doesn’t require React to be in scope, <a href=\"#removing-unused-react-imports\">we’ve also prepared an automated script</a> that will remove the unnecessary imports from your codebase.</p>\n<h3 id=\"create-react-app\"><a href=\"#create-react-app\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create React App </h3>\n<p>Create React App support <a href=\"https://github.com/facebook/create-react-app/pull/9645\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">has been added</a> and will be available in the <a href=\"https://gist.github.com/iansu/4fab7a9bfa5fa6ebc87a908c62f5340b\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">upcoming v4.0 release</a> which is currently in beta testing.</p>\n<h3 id=\"nextjs\"><a href=\"#nextjs\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Next.js </h3>\n<p>Next.js <a href=\"https://github.com/vercel/next.js/releases/tag/v9.5.3\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">v9.5.3</a>+ uses the new transform for compatible React versions.</p>\n<h3 id=\"gatsby\"><a href=\"#gatsby\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Gatsby </h3>\n<p>Gatsby <a href=\"https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/CHANGELOG.md#22452-2020-08-28\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">v2.24.5</a>+ uses the new transform for compatible React versions.</p>\n<blockquote>\n<p>Note</p>\n<p>If you get <a href=\"https://github.com/gatsbyjs/gatsby/issues/26979\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">this Gatsby error</a> after upgrading to React <code class=\"gatsby-code-text\">17.0.0-rc.2</code>, run <code class=\"gatsby-code-text\">npm update</code> to fix it.</p>\n</blockquote>\n<h3 id=\"manual-babel-setup\"><a href=\"#manual-babel-setup\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Manual Babel Setup </h3>\n<p>Support for the new JSX transform is available in Babel <a href=\"https://babeljs.io/blog/2020/03/16/7.9.0\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">v7.9.0</a> and above.</p>\n<p>First, you’ll need to update to the latest Babel and plugin transform.</p>\n<p>If you are using <code class=\"gatsby-code-text\">@babel/plugin-transform-react-jsx</code>:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token comment\"># for npm users</span>\n<span class=\"token function\">npm</span> update @babel/core @babel/plugin-transform-react-jsx</code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token comment\"># for yarn users</span>\n<span class=\"token function\">yarn</span> upgrade @babel/core @babel/plugin-transform-react-jsx</code></pre></div>\n<p>If you are using <code class=\"gatsby-code-text\">@babel/preset-react</code>:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token comment\"># for npm users</span>\n<span class=\"token function\">npm</span> update @babel/core @babel/preset-react</code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token comment\"># for yarn users</span>\n<span class=\"token function\">yarn</span> upgrade @babel/core @babel/preset-react</code></pre></div>\n<p>Currently, the old transform (<code class=\"gatsby-code-text\">&quot;runtime&quot;: &quot;classic&quot;</code>) is the default option. To enable the new transform, you can pass <code class=\"gatsby-code-text\">{&quot;runtime&quot;: &quot;automatic&quot;}</code> as an option to <code class=\"gatsby-code-text\">@babel/plugin-transform-react-jsx</code> or <code class=\"gatsby-code-text\">@babel/preset-react</code>:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token comment\">// If you are using @babel/preset-react</span>\n<span class=\"token punctuation\">{</span>\n  <span class=\"token string\">\"presets\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">[</span><span class=\"token string\">\"@babel/preset-react\"</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token string\">\"runtime\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"automatic\"</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">]</span>\n  <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token comment\">// If you're using @babel/plugin-transform-react-jsx</span>\n<span class=\"token punctuation\">{</span>\n  <span class=\"token string\">\"plugins\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">[</span><span class=\"token string\">\"@babel/plugin-transform-react-jsx\"</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token string\">\"runtime\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"automatic\"</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">]</span>\n  <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Starting from Babel 8, <code class=\"gatsby-code-text\">&quot;automatic&quot;</code> will be the default runtime for both plugins. For more information, check out the Babel documentation for <a href=\"https://babeljs.io/docs/en/babel-plugin-transform-react-jsx\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">@babel/plugin-transform-react-jsx</a> and <a href=\"https://babeljs.io/docs/en/babel-preset-react\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">@babel/preset-react</a>.</p>\n<blockquote>\n<p>Note</p>\n<p>If you use JSX with a library other than React, you can use <a href=\"https://babeljs.io/docs/en/babel-preset-react#importsource\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">the <code class=\"gatsby-code-text\">importSource</code> option</a> to import from that library instead — as long as it provides the necessary entry points. Alternatively, you can keep using the classic transform which will continue to be supported.</p>\n</blockquote>\n<h3 id=\"eslint\"><a href=\"#eslint\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>ESLint </h3>\n<p>If you are using <a href=\"https://github.com/yannickcr/eslint-plugin-react\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">eslint-plugin-react</a>, the <code class=\"gatsby-code-text\">react/jsx-uses-react</code> and <code class=\"gatsby-code-text\">react/react-in-jsx-scope</code> rules are no longer necessary and can be turned off or removed.</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// ...</span>\n  <span class=\"token string\">\"rules\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">// ...</span>\n    <span class=\"token string\">\"react/jsx-uses-react\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"off\"</span><span class=\"token punctuation\">,</span>\n    <span class=\"token string\">\"react/react-in-jsx-scope\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"off\"</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h3 id=\"typescript\"><a href=\"#typescript\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>TypeScript </h3>\n<p>TypeScript supports the JSX transform in <a href=\"https://devblogs.microsoft.com/typescript/announcing-typescript-4-1-beta/#jsx-factories\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">v4.1 beta</a>.</p>\n<h3 id=\"flow\"><a href=\"#flow\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Flow </h3>\n<p>Flow supports the new JSX transform in <a href=\"https://github.com/facebook/flow/releases/tag/v0.126.0\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">v0.126.0</a> and up.</p>\n<h2 id=\"removing-unused-react-imports\"><a href=\"#removing-unused-react-imports\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Removing Unused React Imports </h2>\n<p>Because the new JSX transform will automatically import the necessary <code class=\"gatsby-code-text\">react/jsx-runtime</code> functions, React will no longer need to be in scope when you use JSX. This might lead to unused React imports in your code. It doesn’t hurt to keep them, but if you’d like to remove them, we recommend running a <a href=\"https://medium.com/@cpojer/effective-javascript-codemods-5a6686bb46fb\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">“codemod”</a> script to remove them automatically:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token builtin class-name\">cd</span> your_project\nnpx react-codemod update-react-imports</code></pre></div>\n<blockquote>\n<p>Note</p>\n<p>If you’re getting errors when running the codemod, try specifying a different JavaScript dialect when <code class=\"gatsby-code-text\">npx react-codemod update-react-imports</code> asks you to choose one. In particular, at this moment the “JavaScript with Flow” setting supports newer syntax than the “JavaScript” setting even if you don’t use Flow. <a href=\"https://github.com/reactjs/react-codemod/issues\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">File an issue</a> if you run into problems.</p>\n<p>Keep in mind that the codemod output will not always match your project’s coding style, so you might want to run <a href=\"https://prettier.io/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Prettier</a> after the codemod finishes for consistent formatting.</p>\n</blockquote>\n<p>Running this codemod will:</p>\n<ul>\n<li>Remove all unused React imports as a result of upgrading to the new JSX transform.</li>\n<li>Change all default React imports (i.e. <code class=\"gatsby-code-text\">import React from &quot;react&quot;</code>) to destructured named imports (ex. <code class=\"gatsby-code-text\">import { useState } from &quot;react&quot;</code>) which is the preferred style going into the future. This codemod <strong>will not</strong> affect the existing namespace imports (i.e. <code class=\"gatsby-code-text\">import * as React from &quot;react&quot;</code>) which is also a valid style. The default imports will keep working in React 17, but in the longer term we encourage moving away from them.</li>\n</ul>\n<p>For example,</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">import</span> React <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">App</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">Hello World</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>will be replaced with</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">function</span> <span class=\"token function\">App</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">Hello World</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>If you use some other import from React — for example, a Hook — then the codemod will convert it to a named import.</p>\n<p>For example,</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">import</span> React <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">App</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>text<span class=\"token punctuation\">,</span> setText<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> React<span class=\"token punctuation\">.</span><span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Hello World'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token keyword\">return</span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">{</span>text<span class=\"token punctuation\">}</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>will be replaced with</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> useState <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">App</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>text<span class=\"token punctuation\">,</span> setText<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Hello World'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token keyword\">return</span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">{</span>text<span class=\"token punctuation\">}</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>In addition to cleaning up unused imports, this will also help you prepare for a future major version of React (not React 17) which will support ES Modules and not have a default export.</p>\n<h2 id=\"thanks\"><a href=\"#thanks\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Thanks </h2>\n<p>We’d like to thank Babel, TypeScript, Create React App, Next.js, Gatsby, ESLint, and Flow maintainers for their help implementing and integrating the new JSX transform. We also want to thank the React community for their feedback and discussion on the related <a href=\"https://github.com/reactjs/rfcs/pull/107\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">technical RFC</a>.</p>","excerpt":"Although React 17 doesn’t contain new features, it will provide support for a new version of the JSX transform. In this post, we will describe what it is and how to try it. What’s a JSX Transform?  Browsers don’t understand JSX out of the box, so most React users rely on a compiler like Babel or TypeScript to transform JSX code into regular JavaScript. Many preconfigured toolkits like Create React App or Next.js also include a JSX transform under the hood. Together with the React 17 release, we…","frontmatter":{"title":"Introducing the New JSX Transform","next":null,"prev":null,"author":[{"frontmatter":{"name":"Luna Ruan","url":"https://twitter.com/lunaruan"}}]},"fields":{"date":"September 22, 2020","path":"content/blog/2020-09-22-introducing-the-new-jsx-transform.md","slug":"/blog/2020/09/22/introducing-the-new-jsx-transform.html"}},"allMarkdownRemark":{"edges":[{"node":{"frontmatter":{"title":"Introducing the New JSX Transform"},"fields":{"slug":"/blog/2020/09/22/introducing-the-new-jsx-transform.html"}}},{"node":{"frontmatter":{"title":"React v17.0 Release Candidate: No New Features"},"fields":{"slug":"/blog/2020/08/10/react-v17-rc.html"}}},{"node":{"frontmatter":{"title":"React v16.13.0"},"fields":{"slug":"/blog/2020/02/26/react-v16.13.0.html"}}},{"node":{"frontmatter":{"title":"Building Great User Experiences with Concurrent Mode and Suspense"},"fields":{"slug":"/blog/2019/11/06/building-great-user-experiences-with-concurrent-mode-and-suspense.html"}}},{"node":{"frontmatter":{"title":"Preparing for the Future with React Prereleases"},"fields":{"slug":"/blog/2019/10/22/react-release-channels.html"}}},{"node":{"frontmatter":{"title":"Introducing the New React DevTools"},"fields":{"slug":"/blog/2019/08/15/new-react-devtools.html"}}},{"node":{"frontmatter":{"title":"React v16.9.0 and the Roadmap Update"},"fields":{"slug":"/blog/2019/08/08/react-v16.9.0.html"}}},{"node":{"frontmatter":{"title":"Is React Translated Yet? ¡Sí! Sim! はい！"},"fields":{"slug":"/blog/2019/02/23/is-react-translated-yet.html"}}},{"node":{"frontmatter":{"title":"React v16.8: The One With Hooks"},"fields":{"slug":"/blog/2019/02/06/react-v16.8.0.html"}}},{"node":{"frontmatter":{"title":"React v16.7: No, This Is Not the One With Hooks"},"fields":{"slug":"/blog/2018/12/19/react-v-16-7.html"}}}]}},"pageContext":{"slug":"/blog/2020/09/22/introducing-the-new-jsx-transform.html"}},"staticQueryHashes":[]}