{"id":6996,"date":"2019-05-10T21:38:45","date_gmt":"2019-05-10T21:38:45","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6996"},"modified":"2019-05-10T21:38:45","modified_gmt":"2019-05-10T21:38:45","slug":"jsx-tsx-in-react-what-are-they","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/jsx-tsx-in-react-what-are-they\/","title":{"rendered":"JSX, TSX in React, what are they?"},"content":{"rendered":"<p>React allows us to write JavaScript code in the standard .js files along with TypeScript in the .ts files, but if you&#8217;ve created either a TypeScript or JavaScript React app using yarn&#8217;s create command, i.e.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nyarn create react-app my-app\r\n<\/pre>\n<p>you&#8217;ll notice .jsx or .tsx files. These are similar to Razor, ASP\/ASP.NET, JSP and the likes in that they allow us to embed XML (or strictly speaking in our usage XHTML) into source code. <\/p>\n<p>Let&#8217;s look at a React component (I&#8217;ll use TypeScript in this example but JavaScript jsx components are much the same), in our App.tsx we might want to include our own tag\/element, for example<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;HelloMessage name=&quot;PutridParrot&quot; \/&gt;\r\n<\/pre>\n<p><em>Yes, we&#8217;re always obsessed with the Hello World example and this one&#8217;s no different.<\/em><\/p>\n<p>The above doesn&#8217;t really give any context, so here&#8217;s the whole of the App.tdx file<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nimport React, { Component } from 'react';\r\nimport '.\/App.css';\r\nimport HelloMessage from &quot;.\/components\/HelloMessage&quot;;\r\n\r\nclass App extends Component {\r\n  render() {\r\n    return (\r\n      &lt;div className=&quot;App&quot;&gt;\r\n        &lt;header className=&quot;App-header&quot;&gt;\r\n            &lt;HelloMessage name=&quot;Mark&quot; \/&gt;\r\n        &lt;\/header&gt;\r\n      &lt;\/div&gt;\r\n    );\r\n  }\r\n}\r\n\r\nexport default App;\r\n<\/pre>\n<p>Now the <em>HelloMessage<\/em> element is a React component. In this example I&#8217;ve created a <em>components<\/em> folder off of the src folder and created the file named <em>HelloMessage.tsx<\/em>, let&#8217;s look at the code for this component and then discuss how it works, here&#8217;s the code<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nimport React, { Component } from 'react';\r\n\r\nexport interface HelloMessageProps {name: string;}\r\n\r\nclass HelloMessage extends Component&lt;HelloMessageProps, {}&gt; {\r\n    render() {\r\n      return (\r\n        &lt;div&gt;\r\n          Hello World {this.props.name}\r\n        &lt;\/div&gt;\r\n      );\r\n    }\r\n  }\r\n\r\nexport default HelloMessage;\r\n<\/pre>\n<p>In this example we&#8217;ve imported React, hence need not type React.Component, see <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/blob\/master\/doc\/spec.md#1132-import-declarations\" rel=\"noopener noreferrer\" target=\"_blank\">Import Declarations<\/a> for more information on this syntax.<\/p>\n<p>Next up, we&#8217;ve created and exported an interface. This acts as the <strong>shape<\/strong> of the data that the Component expects to see. As TypeScript supports duck typing, we&#8217;re basically saying we expect a thing that looks like HelloMessageProps (i.e. has a name of type string) to be passed into the component. This is our attribute list from the example in App.tsx.<\/p>\n<p>The HelloMessage class extends\/inherits from Component and we tell it what data shape we expect to be passed into it, but we do not want to pass any state hence we have a second generic parameter of {}. As we&#8217;re writing this as a .tsx we&#8217;ve embedded the XHTML along with the use of the property <em>name<\/em> passed as an attribute in the App.tsx. The <em>render<\/em> method simply returns the embedded XHTML and that&#8217;s it.<\/p>\n<p>We can write the same thing without using the embedded syntax, like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport React, { Component } from 'react';\r\n\r\nexport interface HelloMessageProps {name: string;}\r\n\r\nclass HelloMessage extends Component&lt;HelloMessageProps, {}&gt; {\r\n    render() {\r\n        return React.createElement('div', null, 'Hello World ' + this.props.name);\r\n      }\r\n  }\r\n\r\nexport default HelloMessage;\r\n<\/pre>\n<p>Or better still we can use string interpolation by using the ` (back tick), i.e. replacing the Hello World string section with<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n`Hello World ${this.props.name}`\r\n<\/pre>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/tree\/master\/doc\" rel=\"noopener noreferrer\" target=\"_blank\">TypeScript specification<\/a><br \/>\n<a href=\"https:\/\/reactjs.org\/docs\/react-without-jsx.html\" rel=\"noopener noreferrer\" target=\"_blank\">React without JSX<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>React allows us to write JavaScript code in the standard .js files along with TypeScript in the .ts files, but if you&#8217;ve created either a TypeScript or JavaScript React app using yarn&#8217;s create command, i.e. yarn create react-app my-app you&#8217;ll notice .jsx or .tsx files. These are similar to Razor, ASP\/ASP.NET, JSP and the likes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[243],"tags":[],"class_list":["post-6996","post","type-post","status-publish","format-standard","hentry","category-react"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6996"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6996\/revisions"}],"predecessor-version":[{"id":7024,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6996\/revisions\/7024"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}