{"id":6998,"date":"2019-05-10T21:34:54","date_gmt":"2019-05-10T21:34:54","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6998"},"modified":"2019-05-10T21:34:54","modified_gmt":"2019-05-10T21:34:54","slug":"creating-a-react-application-by-hand","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-a-react-application-by-hand\/","title":{"rendered":"Creating a React application by hand"},"content":{"rendered":"<p>By default it&#8217;s probably better and certainly simpler to create a React application using <em>yarn create<\/em> but it&#8217;s always good to know how to create such an application from scratch yourself.<\/p>\n<p>To start with, create a folder for our application and within it create a folder named src, within this create an empty file named index.tsx. Then add the following to this file<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nReactDOM.render(\r\n  &lt;App \/&gt;,\r\n  document.getElementById('root')\r\n);\r\n<\/pre>\n<p>We don&#8217;t actually have React dependencies available to our application at this point so run the following, these will add React as well as TypeScript and TypeScript definitions of React (obviously if you don&#8217;t want to work in TypeScript, remove the &#8211;typescript switches and the last three yarn commands).<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nyarn add react --typescript\r\nyarn add react-dom --typescript\r\nyarn add react-scripts\r\nyarn add typescript\r\nyarn add @types\/react\r\nyarn add @types\/react-dom\r\n<\/pre>\n<p>yarn will create the package.json file and download the required dependencies. They purpose of the dependencies are probably pretty obvious &#8211; the first includes React, the second React-dom and the third gives us the scripts we&#8217;re used to when running the code generated React applications, such as supplying the script for <em>yarn start<\/em>.<\/p>\n<p>Within the package.json add the following, which will add the scripts that we&#8217;re used to having available<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&quot;scripts&quot;: {\r\n   &quot;start&quot;: &quot;react-scripts start&quot;,\r\n   &quot;build&quot;: &quot;react-scripts build&quot;,\r\n   &quot;test&quot;: &quot;react-scripts test&quot;,\r\n   &quot;eject&quot;: &quot;react-scripts eject&quot;\r\n}\r\n<\/pre>\n<p>We&#8217;re going to need to also add a folder named <em>public<\/em> which we&#8217;ll place an index.html file in which will act as our default page. Here&#8217;s a minimal version copied from a React generated application<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html lang=&quot;en&quot;&gt;\r\n  &lt;head&gt;\r\n    &lt;meta charset=&quot;utf-8&quot; \/&gt;\r\n    &lt;link rel=&quot;shortcut icon&quot; href=&quot;%PUBLIC_URL%\/favicon.ico&quot; \/&gt;\r\n    &lt;meta\r\n      name=&quot;viewport&quot;\r\n      content=&quot;width=device-width, initial-scale=1, shrink-to-fit=no&quot;\r\n    \/&gt;\r\n    &lt;meta name=&quot;theme-color&quot; content=&quot;#000000&quot; \/&gt;\r\n    &lt;link rel=&quot;manifest&quot; href=&quot;%PUBLIC_URL%\/manifest.json&quot; \/&gt;\r\n    &lt;title&gt;Game App&lt;\/title&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;noscript&gt;You need to enable JavaScript to run this app.&lt;\/noscript&gt;\r\n    &lt;div id=&quot;root&quot;&gt;&lt;\/div&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>At this point we should be able to execute<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nyarn start\r\n<\/pre>\n<p>and now we should see a React application running in the browser.<\/p>\n<p>If you want to add some CSS to the application, we can simply create index.css in the src folder, create your styles then to use it in index.tsx add the following at the top of the file<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport '.\/index.css';\r\n<\/pre>\n<p>If you&#8217;re using VSCode, as I am, you may wish to click on the status bar where it display a version of TypeScript, when index.tsx is open, and set to the most upto date version listed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By default it&#8217;s probably better and certainly simpler to create a React application using yarn create but it&#8217;s always good to know how to create such an application from scratch yourself. To start with, create a folder for our application and within it create a folder named src, within this create an empty file named [&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,242],"tags":[],"class_list":["post-6998","post","type-post","status-publish","format-standard","hentry","category-react","category-yarn"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6998","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=6998"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6998\/revisions"}],"predecessor-version":[{"id":7021,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6998\/revisions\/7021"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}