{"id":44,"date":"2021-09-28T11:49:48","date_gmt":"2021-09-28T11:49:48","guid":{"rendered":"https:\/\/www.gadgetappdevelopment.com\/blog\/?page_id=44"},"modified":"2021-10-01T12:15:47","modified_gmt":"2021-10-01T12:15:47","slug":"react-js-beginners","status":"publish","type":"page","link":"https:\/\/www.gadgetappdevelopment.com\/blog\/react-js-beginners\/","title":{"rendered":"React JS Beginners"},"content":{"rendered":"\n<p>There is no consensus on the right way to organize a React application. React The essential guide to getting started with React. This walkthrough tutorial will have you building an application in React from scratch, learning about state, props, and components along the way.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>I&#8217;ve been hearing about React since I first started learning JavaScript, but I&#8217;ll admit I took one look at it and it scared me. I saw what looked like a bunch of HTML mixed with JavaScript and thought,&nbsp;<em>isn&#8217;t this what we&#8217;ve been trying to avoid? What&#8217;s the big deal with React?<\/em><\/p>\n\n\n\n<p>Instead, I focused on just learning vanilla JavaScript and working with jQuery in a professional setting. After a few frustrated, failed attempts to get started with React, I finally started to get it, and I began to see why I might want to use React instead of vanilla JS or jQuery.<\/p>\n\n\n\n<p>I tried to condense everything I&#8217;ve learned into a nice introduction to share with you, so here it is.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"prerequisites\">PREREQUISITES<\/h6>\n\n\n\n<p>There are a few things you should know in advance before you start playing around with React. If you&#8217;ve never used JavaScript or the DOM at all before, for example, I would get more familiar with those before trying to tackle React.<\/p>\n\n\n\n<p>Here are what I consider to be React prerequisites.<\/p>\n\n\n\n<ul><li>Basic familiarity with&nbsp;<a href=\"https:\/\/internetingishard.com\/\">HTML &amp; CSS<\/a>.<\/li><li>Basic knowledge of&nbsp;<a href=\"https:\/\/www.digitalocean.com\/community\/tutorial_series\/how-to-code-in-javascript\">JavaScript<\/a>&nbsp;and programming.<\/li><li>Basic understanding of&nbsp;<a href=\"https:\/\/www.taniarascia.com\/introduction-to-the-dom\/\">the DOM<\/a>.<\/li><li>Familiarity with&nbsp;<a href=\"https:\/\/www.taniarascia.com\/es6-syntax-and-feature-overview\/\">ES6 syntax and features<\/a>.<\/li><li><a href=\"https:\/\/www.taniarascia.com\/how-to-install-and-use-node-js-and-npm-mac-and-windows\/\">Node.js and npm<\/a>&nbsp;installed globally.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"goals\">GOALS<\/h6>\n\n\n\n<ul><li>Learn about essential React concepts and related terms, such as Babel, Webpack, JSX, components, props, state, and lifecycle.<\/li><li>Build a very simple React app that demonstrates the above concepts.<\/li><\/ul>\n\n\n\n<p>Here&#8217;s the source and a live demo of the end result.<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/github.com\/taniarascia\/react-tutorial\">View Source on GitHub<\/a><\/li><li><a href=\"https:\/\/taniarascia.github.io\/react-tutorial\/\">View Demo<\/a><\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"what-is-react\">What is React?<\/h6>\n\n\n\n<ul><li>React is a JavaScript library &#8211; one of the most popular ones, with&nbsp;<a href=\"https:\/\/github.com\/facebook\/react\">over 100,000 stars on GitHub<\/a>.<\/li><li>React is not a framework (unlike Angular, which is more opinionated).<\/li><li>React is an open-source project created by Facebook.<\/li><li>React is used to build user interfaces (UI) on the front end.<\/li><li>React is the&nbsp;<strong>view<\/strong>&nbsp;layer of an MVC application (Model View Controller)<\/li><\/ul>\n\n\n\n<p>One of the most important aspects of React is the fact that you can create&nbsp;<strong>components<\/strong>, which are like custom, reusable HTML elements, to quickly and efficiently build user interfaces. React also streamlines how data is stored and handled, using&nbsp;<strong>state<\/strong>&nbsp;and&nbsp;<strong>props<\/strong>.<\/p>\n\n\n\n<p>We&#8217;ll go over all of this and more throughout the article, so let&#8217;s get started.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"setup-and-installation\">Setup and Installation<\/h6>\n\n\n\n<p>There are a few ways to set up React, and I&#8217;ll show you two so you get a good idea of how it works.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"static-html-file\">Static HTML File<\/h6>\n\n\n\n<p>This first method is not a popular way to set up React and is not how we&#8217;ll be doing the rest of our tutorial, but it will be familiar and easy to understand if you&#8217;ve ever used a library like jQuery, and it&#8217;s the least scary way to get started if you&#8217;re not familiar with Webpack, Babel, and Node.js.<\/p>\n\n\n\n<p>Let&#8217;s start by making a basic&nbsp;<code>index.html<\/code>&nbsp;file. We&#8217;re going to load in three CDNs in the&nbsp;<code>head<\/code>&nbsp;&#8211; React, React DOM, and Babel. We&#8217;re also going to make a&nbsp;<code>div<\/code>&nbsp;with an id called&nbsp;<code>root<\/code>, and finally we&#8217;ll create a&nbsp;<code>script<\/code>&nbsp;tag where your custom code will live.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>index.html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;meta charset=\"utf-8\" \/&gt;\n\n    &lt;title&gt;Hello React!&lt;\/title&gt;\n\n    &lt;script src=\"https:\/\/unpkg.com\/react@^16\/umd\/react.production.min.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/unpkg.com\/react-dom@16.13.0\/umd\/react-dom.production.min.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/unpkg.com\/babel-standalone@6.26.0\/babel.js\"&gt;&lt;\/script&gt;\n  &lt;\/head&gt;\n\n  &lt;body&gt;\n    &lt;div id=\"root\"&gt;&lt;\/div&gt;\n\n    &lt;script type=\"text\/babel\"&gt;\n      \/\/ React code will go here\n    &lt;\/script&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>I&#8217;m loading in the latest stable versions of the libraries as of the time of this writing.<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/reactjs.org\/docs\/react-api.html\">React<\/a>&nbsp;&#8211; the React top level API<\/li><li><a href=\"https:\/\/reactjs.org\/docs\/react-dom.html\">React DOM<\/a>&nbsp;&#8211; adds DOM-specific methods<\/li><li><a href=\"https:\/\/babeljs.io\/\">Babel<\/a>&nbsp;&#8211; a JavaScript compiler that lets us use ES6+ in old browsers<\/li><\/ul>\n\n\n\n<p>The entry point for our app will be the&nbsp;<code>root<\/code>&nbsp;div element, which is named by convention. You&#8217;ll also notice the&nbsp;<code>text\/babel<\/code>&nbsp;script type, which is mandatory for using Babel.<\/p>\n\n\n\n<p>Now, let&#8217;s write our first code block of React. We&#8217;re going to use ES6 classes to create a React component called&nbsp;<code>App<\/code>.<\/p>\n\n\n\n<p>index.html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class App extends React.Component {\n  \/\/...\n}<\/code><\/pre>\n\n\n\n<p>Now we&#8217;ll add the&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/react-component.html#render\"><code>render()<\/code><\/a>&nbsp;method, the only required method in a class component, which is used to render DOM nodes.<\/p>\n\n\n\n<p>index.html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class App extends React.Component {\n  render() {\n      return (\n          \/\/...\n      );\n  }\n}<\/code><\/pre>\n\n\n\n<p>Inside the&nbsp;<code>return<\/code>, we&#8217;re going to put what looks like a simple HTML element. Note that we&#8217;re not returning a string here, so don&#8217;t use quotes around the element. This is called&nbsp;<code>JSX<\/code>, and we&#8217;ll learn more about it soon.<\/p>\n\n\n\n<p>index.html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class App extends React.Component {\n  render() {\n    return &lt;h1&gt;Hello world!&lt;\/h1&gt;\n  }\n}<\/code><\/pre>\n\n\n\n<p>Finally, we&#8217;re going to use the React DOM&nbsp;<code>render()<\/code>&nbsp;method to render the&nbsp;<code>App<\/code>&nbsp;class we created into the&nbsp;<code>root<\/code>&nbsp;div in our HTML.<\/p>\n\n\n\n<p>index.html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ReactDOM.render(&lt;App \/&gt;, document.getElementById('root'))<\/code><\/pre>\n\n\n\n<p>Here is the full code for our&nbsp;<code>index.html<\/code>.<\/p>\n\n\n\n<p>index.html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;meta charset=\"utf-8\" \/&gt;\n\n    &lt;title&gt;Hello React!&lt;\/title&gt;\n\n    &lt;script src=\"https:\/\/unpkg.com\/react@16\/umd\/react.development.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/unpkg.com\/react-dom@16\/umd\/react-dom.development.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/unpkg.com\/babel-standalone@6.26.0\/babel.js\"&gt;&lt;\/script&gt;\n  &lt;\/head&gt;\n\n  &lt;body&gt;\n    &lt;div id=\"root\"&gt;&lt;\/div&gt;\n\n    &lt;script type=\"text\/babel\"&gt;\n      class App extends React.Component {\n        render() {\n          return &lt;h1&gt;Hello world!&lt;\/h1&gt;\n        }\n      }\n\n      ReactDOM.render(&lt;App \/&gt;, document.getElementById('root'))\n    &lt;\/script&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>Now if you view your&nbsp;<code>index.html<\/code>&nbsp;in the browser, you&#8217;ll see the&nbsp;<code>h1<\/code>&nbsp;tag we created rendered to the DOM.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.taniarascia.com\/static\/deb38c3270204d12d046797731ce7ee8\/5a190\/Screen-Shot-2018-08-18-at-10.34.09-AM.png\" alt=\"Screen Shot 2018 08 18 at 10 34 09 AM\"\/><\/figure>\n\n\n\n<p>Cool! Now that you&#8217;ve done this, you can see that React isn&#8217;t so insanely scary to get started with. It&#8217;s just some JavaScript helper libraries that we can load into our HTML.<\/p>\n\n\n\n<p>We&#8217;ve done this for demonstration purposes, but from here out we&#8217;re going to use another method: Create React App.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"create-react-app\">Create React App<\/h6>\n\n\n\n<p>The method I just used of loading JavaScript libraries into a static HTML page and rendering the React and Babel on the fly is not very efficient, and is hard to maintain.<\/p>\n\n\n\n<p>Fortunately, Facebook has created&nbsp;<a href=\"https:\/\/github.com\/facebook\/create-react-app\">Create React App<\/a>, an environment that comes pre-configured with everything you need to build a React app. It will create a live development server, use Webpack to automatically compile React, JSX, and ES6, auto-prefix CSS files, and use ESLint to test and warn about mistakes in the code.<\/p>\n\n\n\n<p>To set up&nbsp;<code>create-react-app<\/code>, run the following code in your terminal, one directory up from where you want the project to live.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx create-react-app react-tutorial<\/code><\/pre>\n\n\n\n<p>Once that finishes installing, move to the newly created directory and start the project.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd react-tutorial &amp;&amp; npm start<\/code><\/pre>\n\n\n\n<p>Once you run this command, a new window will popup at&nbsp;<code>localhost:3000<\/code>&nbsp;with your new React app.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.taniarascia.com\/static\/1c5a36e06f57edfc718276e9ddf9a9c1\/5a190\/Screen-Shot-2018-08-18-at-11.37.59-AM.png\" alt=\"Screen Shot 2018 08 18 at 11 37 59 AM\"\/><\/figure>\n\n\n\n<p>Create React App is very good for getting started for beginners as well as large-scale enterprise applications, but it&#8217;s not perfect for every workflow. You can also create your own Webpack setup for React.<\/p>\n\n\n\n<p>If you look into the project structure, you&#8217;ll see a&nbsp;<code>\/public<\/code>&nbsp;and&nbsp;<code>\/src<\/code>&nbsp;directory, along with the regular&nbsp;<code>node_modules<\/code>,&nbsp;<code>.gitignore<\/code>,&nbsp;<code>README.md<\/code>, and&nbsp;<code>package.json<\/code>.<\/p>\n\n\n\n<p>In&nbsp;<code>\/public<\/code>, our important file is&nbsp;<code>index.html<\/code>, which is very similar to the static&nbsp;<code>index.html<\/code>&nbsp;file we made earlier &#8211; just a&nbsp;<code>root<\/code>&nbsp;div. This time, no libraries or scripts are being loaded in. The&nbsp;<code>\/src<\/code>&nbsp;directory will contain all our React code.<\/p>\n\n\n\n<p>To see how the environment automatically compiles and updates your React code, find the line that looks like this in&nbsp;<code>\/src\/App.js<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>To get started, edit `src\/App.js` and save to reload.<\/code><\/pre>\n\n\n\n<p>For&nbsp;<code>index.css<\/code>, I just copy-and-pasted the contents of&nbsp;<a href=\"https:\/\/taniarascia.github.io\/primitive\/css\/main.css\">Primitive CSS<\/a>&nbsp;into the file. If you want, you can use Bootstrap or whatever CSS framework you want, or nothing at all. I just find it easier to work with.<\/p>\n\n\n\n<p>Now in&nbsp;<code>index.js<\/code>, we&#8217;re importing React, ReactDOM, and the CSS file.<\/p>\n\n\n\n<p>src\/index.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react'\nimport ReactDOM from 'react-dom'\nimport '.\/index.css'<\/code><\/pre>\n\n\n\n<p>Let&#8217;s create our&nbsp;<code>App<\/code>&nbsp;component again. Before, we just had an&nbsp;<code>&lt;h1&gt;<\/code>, but now I&#8217;m adding in a div element with a class as well. You&#8217;ll notice that we use&nbsp;<code>className<\/code>&nbsp;instead of&nbsp;<code>class<\/code>. This is our first hint that the code being written here is JavaScript, and not actually HTML.<\/p>\n\n\n\n<p>src\/index.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class App extends React.Component {\n  render() {\n    return (\n      &lt;div className=\"App\"&gt;\n        &lt;h1&gt;Hello, React!&lt;\/h1&gt;\n      &lt;\/div&gt;\n    )\n  }\n}<\/code><\/pre>\n\n\n\n<p>Finally, we&#8217;ll render the&nbsp;<code>App<\/code>&nbsp;to the root as before.<\/p>\n\n\n\n<p>src\/index.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ReactDOM.render(&lt;App \/&gt;, document.getElementById('root'))<\/code><\/pre>\n\n\n\n<p>Here&#8217;s our full&nbsp;<code>index.js<\/code>. This time, we&#8217;re loading the&nbsp;<code>Component<\/code>&nbsp;as a property of React, so we no longer need to extend&nbsp;<code>React.Component<\/code>.<\/p>\n\n\n\n<p>src\/index.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, {Component} from 'react'\nimport ReactDOM from 'react-dom'\nimport '.\/index.css'\n\nclass App extends Component {\n  render() {\n    return (\n      &lt;div className=\"App\"&gt;\n        &lt;h1&gt;Hello, React!&lt;\/h1&gt;\n      &lt;\/div&gt;\n    )\n  }\n}\n\nReactDOM.render(&lt;App \/&gt;, document.getElementById('root'))<\/code><\/pre>\n\n\n\n<p>If you go back to&nbsp;<code>localhost:3000<\/code>, you&#8217;ll see &#8220;Hello, React!&#8221; just like before. We have the beginnings of a React app now.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"react-developer-tools\">React Developer Tools<\/h6>\n\n\n\n<p>There is an extension called React Developer Tools that will make your life much easier when working with React. Download&nbsp;<a href=\"https:\/\/chrome.google.com\/webstore\/detail\/react-developer-tools\/fmkadmapgofadopljbjfkapdkoienihi\">React DevTools for Chrome<\/a>, or whatever browser you prefer to work on.<\/p>\n\n\n\n<p>After you install it, when you open DevTools, you&#8217;ll see a tab for React. Click on it, and you&#8217;ll be able to inspect components as they&#8217;re written. You can still go to the Elements tab to see the actual DOM output. It may not seem like that much of a deal now, but as the app gets more complicated, it will become increasingly necessary to use.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.taniarascia.com\/static\/888c5b815dcc80ec278cc2c46b563afc\/5a190\/Screen-Shot-2018-08-18-at-3.45.11-PM.png\" alt=\"Screen Shot 2018 08 18 at 3 45 11 PM\"\/><\/figure>\n\n\n\n<p>Now we have all the tools and setup we need to actually begin working with React.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"jsx-javascript--xml\">JSX: JavaScript + XML<\/h6>\n\n\n\n<p>As you&#8217;ve seen, we&#8217;ve been using what looks like HTML in our React code, but it&#8217;s not quite HTML. This is&nbsp;<strong>JSX<\/strong>, which stands for JavaScript XML.<\/p>\n\n\n\n<p>With JSX, we can write what looks like HTML, and also we can create and use our own XML-like tags. Here&#8217;s what JSX looks like assigned to a variable.<\/p>\n\n\n\n<p>JSX<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const heading = &lt;h1 className=\"site-heading\"&gt;Hello, React&lt;\/h1&gt;<\/code><\/pre>\n\n\n\n<p>Using JSX is not mandatory for writing React. Under the hood, it&#8217;s running&nbsp;<code>createElement<\/code>, which takes the tag, object containing the properties, and children of the component and renders the same information. The below code will have the same output as the JSX above.<\/p>\n\n\n\n<p>No JSX<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const heading = React.createElement('h1', {className: 'site-heading'}, 'Hello, React!')<\/code><\/pre>\n\n\n\n<p>JSX is actually closer to JavaScript, not HTML, so there are a few key differences to note when writing it.<\/p>\n\n\n\n<ul><li><code>className<\/code>&nbsp;is used instead of&nbsp;<code>class<\/code>&nbsp;for adding CSS classes, as&nbsp;<code>class<\/code>&nbsp;is a reserved keyword in JavaScript.<\/li><li>Properties and methods in JSX are camelCase &#8211;&nbsp;<code>onclick<\/code>&nbsp;will become&nbsp;<code>onClick<\/code>.<\/li><li>Self-closing tags&nbsp;<em>must<\/em>&nbsp;end in a slash &#8211; e.g.&nbsp;<code>&lt;img \/&gt;<\/code><\/li><\/ul>\n\n\n\n<p>JavaScript expressions can also be embedded inside JSX using curly braces, including variables, functions, and properties.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const name = 'Tania'\nconst heading = &lt;h1&gt;Hello, {name}&lt;\/h1&gt;<\/code><\/pre>\n\n\n\n<p>JSX is easier to write and understand than creating and appending many elements in vanilla JavaScript, and is one of the reasons people love React so much.<\/p>\n\n\n\n<h6 id=\"components\">Components<\/h6>\n\n\n\n<p>So far, we&#8217;ve created one component &#8211; the&nbsp;<code>App<\/code>&nbsp;component. Almost everything in React consists of components, which can be&nbsp;<strong>class components<\/strong>&nbsp;or&nbsp;<strong>simple components<\/strong>.<\/p>\n\n\n\n<p>Most React apps have many small components, and everything loads into the main&nbsp;<code>App<\/code>&nbsp;component. Components also often get their own file, so let&#8217;s change up our project to do so.<\/p>\n\n\n\n<p>Remove the&nbsp;<code>App<\/code>&nbsp;class from&nbsp;<code>index.js<\/code>, so it looks like this.<\/p>\n\n\n\n<p>src\/index.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react'\nimport ReactDOM from 'react-dom'\nimport App from '.\/App'\nimport '.\/index.css'\n\nReactDOM.render(&lt;App \/&gt;, document.getElementById('root'))<\/code><\/pre>\n\n\n\n<p>We&#8217;ll create a new file called&nbsp;<code>App.js<\/code>&nbsp;and put the component in there.<\/p>\n\n\n\n<p>src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, {Component} from 'react'\n\nclass App extends Component {\n  render() {\n    return (\n      &lt;div className=\"App\"&gt;\n        &lt;h1&gt;Hello, React!&lt;\/h1&gt;\n      &lt;\/div&gt;\n    )\n  }\n}\n\nexport default App<\/code><\/pre>\n\n\n\n<p>We export the component as&nbsp;<code>App<\/code>&nbsp;and load it in&nbsp;<code>index.js<\/code>. It&#8217;s not mandatory to separate components into files, but an application will start to get unwieldy and out-of-hand if you don&#8217;t.<\/p>\n\n\n\n<h6 id=\"class-components\">Class Components<\/h6>\n\n\n\n<p>Let&#8217;s create another component. We&#8217;re going to create a table. Make&nbsp;<code>Table.js<\/code>, and fill it with the following data.<\/p>\n\n\n\n<p>src\/Table.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, {Component} from 'react'\n\nclass Table extends Component {\n  render() {\n    return (\n      &lt;table&gt;\n        &lt;thead&gt;\n          &lt;tr&gt;\n            &lt;th&gt;Name&lt;\/th&gt;\n            &lt;th&gt;Job&lt;\/th&gt;\n          &lt;\/tr&gt;\n        &lt;\/thead&gt;\n        &lt;tbody&gt;\n          &lt;tr&gt;\n            &lt;td&gt;Charlie&lt;\/td&gt;\n            &lt;td&gt;Janitor&lt;\/td&gt;\n          &lt;\/tr&gt;\n          &lt;tr&gt;\n            &lt;td&gt;Mac&lt;\/td&gt;\n            &lt;td&gt;Bouncer&lt;\/td&gt;\n          &lt;\/tr&gt;\n          &lt;tr&gt;\n            &lt;td&gt;Dee&lt;\/td&gt;\n            &lt;td&gt;Aspiring actress&lt;\/td&gt;\n          &lt;\/tr&gt;\n          &lt;tr&gt;\n            &lt;td&gt;Dennis&lt;\/td&gt;\n            &lt;td&gt;Bartender&lt;\/td&gt;\n          &lt;\/tr&gt;\n        &lt;\/tbody&gt;\n      &lt;\/table&gt;\n    )\n  }\n}\n\nexport default Table<\/code><\/pre>\n\n\n\n<p>This component we created is a custom class component. We capitalize custom components to differentiate them from regular HTML elements. Back in&nbsp;<code>App.js<\/code>, we can load in the Table, first by importing it in:<\/p>\n\n\n\n<p>src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import Table from '.\/Table'<\/code><\/pre>\n\n\n\n<p>Then by loading it into the&nbsp;<code>render()<\/code>&nbsp;of&nbsp;<code>App<\/code>, where before we had &#8220;Hello, React!&#8221;. I also changed the class of the outer container.<\/p>\n\n\n\n<p>src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, {Component} from 'react'\nimport Table from '.\/Table'\n\nclass App extends Component {\n  render() {\n    return (\n      &lt;div className=\"container\"&gt;\n        &lt;Table \/&gt;\n      &lt;\/div&gt;\n    )\n  }\n}\n\nexport default App<\/code><\/pre>\n\n\n\n<p>If you check back on your live environment, you&#8217;ll see the&nbsp;<code>Table<\/code>&nbsp;loaded in.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.taniarascia.com\/static\/2b0e0f127e4d959b70f8d325f368f196\/5a190\/Screen-Shot-2018-08-18-at-6.10.55-PM.png\" alt=\"Screen Shot 2018 08 18 at 6 10 55 PM\"\/><\/figure>\n\n\n\n<p>Now we&#8217;ve seen what a custom class component is. We could reuse this component over and over. However, since the data is hard-coded into it, it wouldn&#8217;t be too useful at the moment.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"simple-components\">Simple Components<\/h6>\n\n\n\n<p>The other type of component in React is the&nbsp;<strong>simple component<\/strong>, which is a function. This component doesn&#8217;t use the&nbsp;<code>class<\/code>&nbsp;keyword. Let&#8217;s take our&nbsp;<code>Table<\/code>&nbsp;and make two simple components for it &#8211; a table header, and a table body.<\/p>\n\n\n\n<p>We&#8217;re going to use ES6 arrow functions to create these simple components. First, the table header.<\/p>\n\n\n\n<p>src\/Table.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const TableHeader = () =&gt; {\n  return (\n    &lt;thead&gt;\n      &lt;tr&gt;\n        &lt;th&gt;Name&lt;\/th&gt;\n        &lt;th&gt;Job&lt;\/th&gt;\n      &lt;\/tr&gt;\n    &lt;\/thead&gt;\n  )\n}<\/code><\/pre>\n\n\n\n<p>Then the body.<\/p>\n\n\n\n<p>src\/Table.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const TableBody = () =&gt; {\n  return (\n    &lt;tbody&gt;\n      &lt;tr&gt;\n        &lt;td&gt;Charlie&lt;\/td&gt;\n        &lt;td&gt;Janitor&lt;\/td&gt;\n      &lt;\/tr&gt;\n      &lt;tr&gt;\n        &lt;td&gt;Mac&lt;\/td&gt;\n        &lt;td&gt;Bouncer&lt;\/td&gt;\n      &lt;\/tr&gt;\n      &lt;tr&gt;\n        &lt;td&gt;Dee&lt;\/td&gt;\n        &lt;td&gt;Aspiring actress&lt;\/td&gt;\n      &lt;\/tr&gt;\n      &lt;tr&gt;\n        &lt;td&gt;Dennis&lt;\/td&gt;\n        &lt;td&gt;Bartender&lt;\/td&gt;\n      &lt;\/tr&gt;\n    &lt;\/tbody&gt;\n  )\n}<\/code><\/pre>\n\n\n\n<p>Now our&nbsp;<code>Table<\/code>&nbsp;file will look like this. Note that the&nbsp;<code>TableHeader<\/code>&nbsp;and&nbsp;<code>TableBody<\/code>&nbsp;components are all in the same file, and being used by the&nbsp;<code>Table<\/code>&nbsp;class component.<\/p>\n\n\n\n<p>src\/Table.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const TableHeader = () =&gt; { ... }\nconst TableBody = () =&gt; { ... }\n\nclass Table extends Component {\n  render() {\n    return (\n      &lt;table&gt;\n        &lt;TableHeader \/&gt;\n        &lt;TableBody \/&gt;\n      &lt;\/table&gt;\n    )\n  }\n}<\/code><\/pre>\n\n\n\n<p>Everything should appear as it did before. As you can see, components can be nested in other components, and simple and class components can be mixed.<\/p>\n\n\n\n<p>A class component must include&nbsp;<code>render()<\/code>, and the&nbsp;<code>return<\/code>&nbsp;can only return one parent element.<\/p>\n\n\n\n<p>As a wrap up, let&#8217;s compare a simple component with a class component.<\/p>\n\n\n\n<p>Simple Component<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const SimpleComponent = () =&gt; {\n  return &lt;div&gt;Example&lt;\/div&gt;\n}<\/code><\/pre>\n\n\n\n<p>Class Component<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ClassComponent extends Component {\n  render() {\n    return &lt;div&gt;Example&lt;\/div&gt;\n  }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"props\">Props<\/h6>\n\n\n\n<p>Right now, we have a cool&nbsp;<code>Table<\/code>&nbsp;component, but the data is being hard-coded. One of the big deals about React is how it handles data, and it does so with properties, referred to as&nbsp;<strong>props<\/strong>, and with state. Now, we&#8217;ll focus on handling data with props.<\/p>\n\n\n\n<p>First, let&#8217;s remove all the data from our&nbsp;<code>TableBody<\/code>&nbsp;component.<\/p>\n\n\n\n<p>Note that if the&nbsp;<code>return<\/code>&nbsp;is contained to one line, it does not need parentheses.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>src\/Table.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const TableBody = () =&gt; {\n  return &lt;tbody \/&gt;\n}<\/code><\/pre>\n\n\n\n<p>Then let&#8217;s move all that data to an array of objects, as if we were bringing in a JSON-based API. We&#8217;ll have to create this array inside our&nbsp;<code>render()<\/code>.<\/p>\n\n\n\n<p>src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class App extends Component {\n  render() {\n    const characters = &#91;\n      {\n        name: 'Charlie',\n        job: 'Janitor',\n      },\n      {\n        name: 'Mac',\n        job: 'Bouncer',\n      },\n      {\n        name: 'Dee',\n        job: 'Aspring actress',\n      },\n      {\n        name: 'Dennis',\n        job: 'Bartender',\n      },\n    ]\n\n    return (\n      &lt;div className=\"container\"&gt;\n        &lt;Table \/&gt;\n      &lt;\/div&gt;\n    )\n  }\n}<\/code><\/pre>\n\n\n\n<p>Now, we&#8217;re going to pass the data through to the child component (<code>Table<\/code>) with properties, kind of how you might pass data through using&nbsp;<code>data-<\/code>&nbsp;attributes. We can call the property whatever we want, as long as it&#8217;s not a reserved keyword, so I&#8217;ll go with&nbsp;<code>characterData<\/code>. The data I&#8217;m passing through is the&nbsp;<code>characters<\/code>&nbsp;variable, and I&#8217;ll put curly braces around it as it&#8217;s a JavaScript expression.<\/p>\n\n\n\n<p>src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return (\n  &lt;div className=\"container\"&gt;\n    &lt;Table characterData={characters} \/&gt;\n  &lt;\/div&gt;\n)<\/code><\/pre>\n\n\n\n<p>Now that data is being passed through to&nbsp;<code>Table<\/code>, we have to work on accessing it from the other side.<\/p>\n\n\n\n<p>src\/Table.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Table extends Component {\n  render() {\n    const {characterData} = this.props\n\n    return (\n      &lt;table&gt;\n        &lt;TableHeader \/&gt;\n        &lt;TableBody characterData={characterData} \/&gt;\n      &lt;\/table&gt;\n    )\n  }\n}<\/code><\/pre>\n\n\n\n<p>If you open up React DevTools and inspect the&nbsp;<code>Table<\/code>&nbsp;component, you&#8217;ll see the array of data in the property. The data that&#8217;s stored here is known as the&nbsp;<strong>virtual DOM<\/strong>, which is a fast and efficient way of syncing data with the actual DOM.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.taniarascia.com\/static\/4a473290bef5435fb9aa878316de06ef\/5a190\/Screen-Shot-2018-08-19-at-5.43.39-PM.png\" alt=\"Screen Shot 2018 08 19 at 5 43 39 PM\"\/><\/figure>\n\n\n\n<p>This data is not in the actual DOM yet, though. In&nbsp;<code>Table<\/code>, we can access all props through&nbsp;<code>this.props<\/code>. We&#8217;re only passing one props through, characterData, so we&#8217;ll use&nbsp;<code>this.props.characterData<\/code>&nbsp;to retrieve that data.<\/p>\n\n\n\n<p>I&#8217;m going to use the ES6 property shorthand to create a variable that contains&nbsp;<code>this.props.characterData<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const {characterData} = this.props<\/code><\/pre>\n\n\n\n<p>Since our&nbsp;<code>Table<\/code>&nbsp;component actually consists of two smaller simple components, I&#8217;m going to pass it through to the&nbsp;<code>TableBody<\/code>, once again through props.<\/p>\n\n\n\n<p>src\/Table.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Table extends Component {\n  render() {\n    const {characterData} = this.props\n\n    return (\n      &lt;table&gt;\n        &lt;TableHeader \/&gt;\n        &lt;TableBody characterData={characterData} \/&gt;\n      &lt;\/table&gt;\n    )\n  }\n}<\/code><\/pre>\n\n\n\n<p>Right now,&nbsp;<code>TableBody<\/code>&nbsp;takes no parameters and returns a single tag.<\/p>\n\n\n\n<p>src\/Table.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const TableBody = () =&gt; {\n  return &lt;tbody \/&gt;\n}<\/code><\/pre>\n\n\n\n<p>We&#8217;re going to pass the props through as a parameter, and&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/map\">map through the array<\/a>&nbsp;to return a table row for each object in the array. This map will be contained in the&nbsp;<code>rows<\/code>&nbsp;variable, which we&#8217;ll return as an expression.<\/p>\n\n\n\n<p>src\/Table.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const TableBody = (props) =&gt; {\n  const rows = props.characterData.map((row, index) =&gt; {\n    return (\n      &lt;tr key={index}&gt;\n        &lt;td&gt;{row.name}&lt;\/td&gt;\n        &lt;td&gt;{row.job}&lt;\/td&gt;\n      &lt;\/tr&gt;\n    )\n  })\n\n  return &lt;tbody&gt;{rows}&lt;\/tbody&gt;\n}<\/code><\/pre>\n\n\n\n<p>If you view the front end of the app, all the data is loading in now.<\/p>\n\n\n\n<p>You&#8217;ll notice I&#8217;ve added a key index to each table row. You should always use&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html#keys\">keys<\/a>&nbsp;when making lists in React, as they help identify each list item. We&#8217;ll also see how this is necessary in a moment when we want to manipulate list items.<\/p>\n\n\n\n<p>Props are an effective way to pass existing data to a React component, however the component cannot change the props &#8211; they&#8217;re read-only. In the next section, we&#8217;ll learn how to use state to have further control over handling data in React.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"state\">State<\/h6>\n\n\n\n<p>Right now, we&#8217;re storing our character data in an array in a variable, and passing it through as props. This is good to start, but imagine if we want to be able to delete an item from the array. With props, we have a one way data flow, but with state we can update private data from a component.<\/p>\n\n\n\n<p>You can think of state as any data that should be saved and modified without necessarily being added to a database &#8211; for example, adding and removing items from a shopping cart before confirming your purchase.<\/p>\n\n\n\n<p>To start, we&#8217;re going to create a&nbsp;<code>state<\/code>&nbsp;object.<\/p>\n\n\n\n<p>src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class App extends Component {\n  state = {}\n}<\/code><\/pre>\n\n\n\n<p>The object will contain properties for everything you want to store in the state. For us, it&#8217;s&nbsp;<code>characters<\/code>.<\/p>\n\n\n\n<p>src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class App extends Component {\n  state = {\n    characters: &#91;],\n  }\n}<\/code><\/pre>\n\n\n\n<p>Move the entire array of objects we created earlier into&nbsp;<code>state.characters<\/code>.src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class App extends Component {\n  state = {\n    characters: &#91;\n      {\n        name: 'Charlie',\n        \/\/ the rest of the data\n      },\n    ],\n  }\n}<\/code><\/pre>\n\n\n\n<p>Our data is officially contained in the state. Since we want to be able to remove a character from the table, we&#8217;re going to create a&nbsp;<code>removeCharacter<\/code>&nbsp;method on the parent&nbsp;<code>App<\/code>&nbsp;class.<\/p>\n\n\n\n<p>To retrieve the state, we&#8217;ll get&nbsp;<code>this.state.characters<\/code>&nbsp;using the same ES6 method as before. To update the state, we&#8217;ll use&nbsp;<code>this.setState()<\/code>, a built-in method for manipulating state. We&#8217;ll&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/filter\">filter the array<\/a>&nbsp;based on an&nbsp;<code>index<\/code>&nbsp;that we pass through, and return the new array.<\/p>\n\n\n\n<p>You must use&nbsp;<code>this.setState()<\/code>&nbsp;to modify an array. Simply applying a new value to&nbsp;<code>this.state.property<\/code>&nbsp;will not work.<\/p>\n\n\n\n<p>src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>removeCharacter = (index) =&gt; {\n  const {characters} = this.state\n\n  this.setState({\n    characters: characters.filter((character, i) =&gt; {\n      return i !== index\n    }),\n  })\n}<\/code><\/pre>\n\n\n\n<p><code>filter<\/code>&nbsp;does not mutate but rather creates a new array, and is a preferred method for modifying arrays in JavaScript. This particular method is testing an index vs. all the indices in the array, and returning all but the one that is passed through.<\/p>\n\n\n\n<p>Now we have to pass that function through to the component, and render a button next to each character that can invoke the function. We&#8217;ll pass the&nbsp;<code>removeCharacter<\/code>&nbsp;function through as a prop to&nbsp;<code>Table<\/code>.src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>render() {\n  const { characters } = this.state\n\n  return (\n    &lt;div className=\"container\"&gt;\n      &lt;Table characterData={characters} removeCharacter={this.removeCharacter} \/&gt;\n    &lt;\/div&gt;\n  )\n}<\/code><\/pre>\n\n\n\n<p>Since we&#8217;re passing it down to&nbsp;<code>TableBody<\/code>&nbsp;from&nbsp;<code>Table<\/code>, we&#8217;re going to have to pass it through again as a prop, just like we did with the character data.<\/p>\n\n\n\n<p>In addition, since it turns out that the only components having their own states in our project are&nbsp;<code>App<\/code>&nbsp;and&nbsp;<code>Form<\/code>, it would be best practice to transform&nbsp;<code>Table<\/code>&nbsp;into a simple component from the class component it currently is.<\/p>\n\n\n\n<p>src\/Table.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const Table = (props) =&gt; {\n  const {characterData, removeCharacter} = props\n\n  return (\n    &lt;table&gt;\n      &lt;TableHeader \/&gt;\n      &lt;TableBody characterData={characterData} removeCharacter={removeCharacter} \/&gt;\n    &lt;\/table&gt;\n  )\n}<\/code><\/pre>\n\n\n\n<p>Here&#8217;s where that index we defined in the&nbsp;<code>removeCharacter()<\/code>&nbsp;method comes in. In the&nbsp;<code>TableBody<\/code>&nbsp;component, we&#8217;ll pass the key\/index through as a parameter, so the filter function knows which item to remove. We&#8217;ll create a button with an&nbsp;<code>onClick<\/code>&nbsp;and pass it through.<\/p>\n\n\n\n<p>src\/Table.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;tr key={index}&gt;\n  &lt;td&gt;{row.name}&lt;\/td&gt;\n  &lt;td&gt;{row.job}&lt;\/td&gt;\n  &lt;td&gt;\n    &lt;button onClick={() =&gt; props.removeCharacter(index)}&gt;Delete&lt;\/button&gt;\n  &lt;\/td&gt;\n&lt;\/tr&gt;<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>onClick<\/code>&nbsp;function must pass through a function that returns the&nbsp;<code>removeCharacter()<\/code>&nbsp;method, otherwise it will try to run automatically.<\/p>\n\n\n\n<p>Awesome. Now we have delete buttons, and we can modify our state by deleting a character.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.taniarascia.com\/static\/a314736828868b35d906cd426cbb7234\/5a190\/Screen-Shot-2018-08-19-at-6.37.09-PM.png\" alt=\"Screen Shot 2018 08 19 at 6 37 09 PM\"\/><\/figure>\n\n\n\n<p>I deleted Mac.<\/p>\n\n\n\n<p>Now you should understand how state gets initialized and how it can be modified.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"submitting-form-data\">Submitting Form Data<\/h6>\n\n\n\n<p>Now we have data stored in state, and we can remove any item from the state. However, what if we wanted to be able to add new data to state? In a real world application, you&#8217;d more likely start with empty state and add to it, such as with a to-do list or a shopping cart.<\/p>\n\n\n\n<p>Before anything else, let&#8217;s remove all the hard-coded data from&nbsp;<code>state.characters<\/code>, as we&#8217;ll be updating that through the form now.<\/p>\n\n\n\n<p>src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class App extends Component {\n  state = {\n    characters: &#91;],\n  }\n}<\/code><\/pre>\n\n\n\n<p>Now let&#8217;s go ahead and create a&nbsp;<code>Form<\/code>&nbsp;component in a new file called&nbsp;<code>Form.js<\/code>.<\/p>\n\n\n\n<p>We&#8217;re going to set the initial state of the&nbsp;<code>Form<\/code>&nbsp;to be an object with some empty properties, and assign that initial state to&nbsp;<code>this.state<\/code>.src\/Form.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, {Component} from 'react'\n\nclass Form extends Component {\n  initialState = {\n    name: '',\n    job: '',\n  }\n\n  state = this.initialState\n}<\/code><\/pre>\n\n\n\n<p>Previously, it was necessary to include a&nbsp;<code>constructor()<\/code>&nbsp;on React class components, but it&#8217;s not required anymore.<\/p>\n\n\n\n<p>Our goal for this form will be to update the state of&nbsp;<code>Form<\/code>&nbsp;every time a field is changed in the form, and when we submit, all that data will pass to the&nbsp;<code>App<\/code>&nbsp;state, which will then update the&nbsp;<code>Table<\/code>.<\/p>\n\n\n\n<p>First, we&#8217;ll make the function that will run every time a change is made to an input. The&nbsp;<code>event<\/code>&nbsp;will be passed through, and we&#8217;ll set the state of&nbsp;<code>Form<\/code>&nbsp;to have the&nbsp;<code>name<\/code>&nbsp;(key) and&nbsp;<code>value<\/code>&nbsp;of the inputs.src\/Form.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>handleChange = (event) =&gt; {\n  const {name, value} = event.target\n\n  this.setState({\n    &#91;name]: value,\n  })\n}<\/code><\/pre>\n\n\n\n<p>Let&#8217;s get this working before we move on to submitting the form. In the render, let&#8217;s get our two properties from state, and assign them as the values that correspond to the proper form keys. We&#8217;ll run the&nbsp;<code>handleChange()<\/code>&nbsp;method as the&nbsp;<code>onChange<\/code>&nbsp;of the input, and finally we&#8217;ll export the&nbsp;<code>Form<\/code>&nbsp;component.src\/Form.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>render() {\n  const { name, job } = this.state;\n\n  return (\n    &lt;form&gt;\n      &lt;label htmlFor=\"name\"&gt;Name&lt;\/label&gt;\n      &lt;input\n        type=\"text\"\n        name=\"name\"\n        id=\"name\"\n        value={name}\n        onChange={this.handleChange} \/&gt;\n      &lt;label htmlFor=\"job\"&gt;Job&lt;\/label&gt;\n      &lt;input\n        type=\"text\"\n        name=\"job\"\n        id=\"job\"\n        value={job}\n        onChange={this.handleChange} \/&gt;\n    &lt;\/form&gt;\n  );\n}\n\nexport default Form;<\/code><\/pre>\n\n\n\n<p>In&nbsp;<code>App.js<\/code>, we can render the form below the table.src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import Form from '.\/Form'<\/code><\/pre>\n\n\n\n<p>src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return (\n  &lt;div className=\"container\"&gt;\n    &lt;Table characterData={characters} removeCharacter={this.removeCharacter} \/&gt;\n    &lt;Form \/&gt;\n  &lt;\/div&gt;\n)<\/code><\/pre>\n\n\n\n<p>Now if we go to the front end of our app, we&#8217;ll see a form that doesn&#8217;t have a submit yet. Update some fields and you&#8217;ll see the local state of&nbsp;<code>Form<\/code>&nbsp;being updated.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.taniarascia.com\/static\/faf2c33e4383a065a6d5d0705b48658c\/5a190\/Screen-Shot-2018-08-19-at-7.55.56-PM.png\" alt=\"Screen Shot 2018 08 19 at 7 55 56 PM\"\/><\/figure>\n\n\n\n<p>Cool. Last step is to allow us to actually submit that data and update the parent state. We&#8217;ll create a function called\u00a0<code>handleSubmit()<\/code>\u00a0on\u00a0<code>App<\/code>\u00a0that will update the state by taking the existing\u00a0<code>this.state.characters<\/code>\u00a0and adding the new\u00a0<code>character<\/code>\u00a0parameter, using the\u00a0<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Spread_syntax\">ES6 spread operator<\/a>.<\/p>\n\n\n\n<p>src\/App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>handleSubmit = (character) =&gt; {\n  this.setState({characters: &#91;...this.state.characters, character]})\n}<\/code><\/pre>\n\n\n\n<p>Let&#8217;s make sure we pass that through as a parameter on&nbsp;<code>Form<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Form handleSubmit={this.handleSubmit} \/&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Form handleSubmit={this.handleSubmit} \/&gt;<\/code><\/pre>\n\n\n\n<p>Now in&nbsp;<code>Form<\/code>, we&#8217;ll create a method called&nbsp;<code>submitForm()<\/code>&nbsp;that will call that function, and pass the&nbsp;<code>Form<\/code>&nbsp;state through as the&nbsp;<code>character<\/code>&nbsp;parameter we defined earlier. It will also reset the state to the initial state, to clear the form after submit.src\/Form.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>submitForm = () =&gt; {\n  this.props.handleSubmit(this.state)\n  this.setState(this.initialState)\n}<\/code><\/pre>\n\n\n\n<p>Finally, we&#8217;ll add a submit button to submit the form. We&#8217;re using an&nbsp;<code>onClick<\/code>&nbsp;instead of an&nbsp;<code>onSubmit<\/code>&nbsp;since we&#8217;re not using the standard submit functionality. The click will call the&nbsp;<code>submitForm<\/code>&nbsp;we just made.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;input type=\"button\" value=\"Submit\" onClick={this.submitForm} \/&gt;<\/code><\/pre>\n\n\n\n<p>And that&#8217;s it! The app is complete. We can create, add, and remove users from our table. Since the\u00a0<code>Table<\/code>\u00a0and\u00a0<code>TableBody<\/code>\u00a0were already pulling from the state, it will display properly.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.taniarascia.com\/static\/5d0c857d6373c593016654a4f40fe9f3\/5a190\/Screen-Shot-2018-08-19-at-9.33.59-PM.png\" alt=\"Screen Shot 2018 08 19 at 9 33 59 PM\"\/><\/figure>\n\n\n\n<p>If you got lost anywhere along the way, you can view\u00a0<a href=\"https:\/\/github.com\/taniarascia\/react-tutorial\">the complete source on GitHub<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"pulling-in-api-data\">Pulling in API Data<\/h6>\n\n\n\n<p>One very common usage of React is pulling in data from an API. If you&#8217;re not familiar with what an API is or how to connect to one, I would recommend reading&nbsp;<a href=\"https:\/\/www.taniarascia.com\/how-to-connect-to-an-api-with-javascript\/\">How to Connect to an API with JavaScript<\/a>, which will walk you through what APIs are and how to use them with vanilla JavaScript.<\/p>\n\n\n\n<p>As a little test, we can create a new&nbsp;<code>Api.js<\/code>&nbsp;file, and create a new&nbsp;<code>App<\/code>&nbsp;in there. A public API we can test with is the&nbsp;<a href=\"https:\/\/en.wikipedia.org\/w\/api.php\">Wikipedia API<\/a>, and I have a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/w\/api.php?action=opensearch&amp;search=Seona+Dancing&amp;format=json&amp;origin=*\">URL endpoint right here<\/a>&nbsp;for a random* search. You can go to that link to see the API &#8211; and make sure you have&nbsp;<a href=\"https:\/\/chrome.google.com\/webstore\/detail\/jsonview\/chklaanhfefbnpoihckbnefhakgolnmc\">JSONView<\/a>&nbsp;installed on your browser.<\/p>\n\n\n\n<p>We&#8217;re going to use&nbsp;<a href=\"https:\/\/www.taniarascia.com\/how-to-use-the-javascript-fetch-api-to-get-json-data\/\">JavaScript&#8217;s built-in Fetch<\/a>&nbsp;to gather the data from that URL endpoint and display it. You can switch between the app we created and this test file by just changing the URL in&nbsp;<code>index.js<\/code>&nbsp;&#8211;&nbsp;<code>import App from '.\/Api';<\/code>.<\/p>\n\n\n\n<p>I&#8217;m not going to explain this code line-by-line, as we&#8217;ve already learned about creating a component, rendering, and mapping through a state array. The new aspect to this code is&nbsp;<code>componentDidMount()<\/code>, a React lifecycle method.&nbsp;<strong>Lifecycle<\/strong>&nbsp;is the order in which methods are called in React.&nbsp;<strong>Mounting<\/strong>&nbsp;refers to an item being inserted into the DOM.<\/p>\n\n\n\n<p>When we pull in API data, we want to use&nbsp;<code>componentDidMount<\/code>, because we want to make sure the component has rendered to the DOM before we bring in the data. In the below snippet, you&#8217;ll see how we bring in data from the Wikipedia API, and display it on the pageApi.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, {Component} from 'react'\n\nclass App extends Component {\n  state = {\n    data: &#91;],\n  }\n\n  \/\/ Code is invoked after the component is mounted\/inserted into the DOM tree.\n  componentDidMount() {\n    const url =\n      'https:\/\/en.wikipedia.org\/w\/api.php?action=opensearch&amp;search=Seona+Dancing&amp;format=json&amp;origin=*'\n\n    fetch(url)\n      .then((result) =&gt; result.json())\n      .then((result) =&gt; {\n        this.setState({\n          data: result,\n        })\n      })\n  }\n\n  render() {\n    const {data} = this.state\n\n    const result = data.map((entry, index) =&gt; {\n      return &lt;li key={index}&gt;{entry}&lt;\/li&gt;\n    })\n\n    return &lt;ul&gt;{result}&lt;\/ul&gt;\n  }\n}\n\nexport default App<\/code><\/pre>\n\n\n\n<p>Once you save and run this file in the local server, you&#8217;ll see the Wikipedia API data displayed in the DOM.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.taniarascia.com\/static\/a76c1cea3c47b440f00a75fb2d004084\/5a190\/Screen-Shot-2018-08-19-at-10.12.41-PM.png\" alt=\"Screen Shot 2018 08 19 at 10 12 41 PM\"\/><\/figure>\n\n\n\n<p>There are other lifecycle methods, but going over them will be beyond the scope of this article. You can&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/react-component.html\">read more about React components here<\/a>.<\/p>\n\n\n\n<p><em>*Wikipedia search choice may not be random. It might be an article that I spearheaded back in 2005.<\/em><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"building-and-deploying-a-react-app\">Building and Deploying a React App<\/h6>\n\n\n\n<p>Everything we&#8217;ve done so far has been in a development environment. We&#8217;ve been compiling, hot-reloading, and updating on the fly. For production, we&#8217;re going to want to have static files loading in &#8211; none of the source code. We can do this by making a build and deploying it.<\/p>\n\n\n\n<p>Now, if you just want to compile all the React code and place it in the root of a directory somewhere, all you need to do is run the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run build<\/code><\/pre>\n\n\n\n<p>This will create a&nbsp;<code>build<\/code>&nbsp;folder which will contain your app. Put the contents of that folder anywhere, and you&#8217;re done!<\/p>\n\n\n\n<p>We can also take it a step further, and have npm deploy for us. We&#8217;re going to build to GitHub pages, so you&#8217;ll already have to&nbsp;<a href=\"https:\/\/www.taniarascia.com\/getting-started-with-git\/\">be familiar with Git<\/a>&nbsp;and getting your code up on GitHub.<\/p>\n\n\n\n<p>Make sure you&#8217;ve exited out of your local React environment, so the code isn&#8217;t currently running. First, we&#8217;re going to add a&nbsp;<code>homepage<\/code>&nbsp;field to&nbsp;<code>package.json<\/code>, that has the URL we want our app to live on.package.json<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"homepage\": \"https:\/\/taniarascia.github.io\/react-tutorial\",<\/code><\/pre>\n\n\n\n<p>We&#8217;ll also add these two lines to the&nbsp;<code>scripts<\/code>&nbsp;property.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"scripts\": {\n  \/\/ ...\n  \"predeploy\": \"npm run build\",\n  \"deploy\": \"gh-pages -d build\"\n}<\/code><\/pre>\n\n\n\n<p>In your project, you&#8217;ll add&nbsp;<code>gh-pages<\/code>&nbsp;to the devDependencies.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install --save-dev gh-pages<\/code><\/pre>\n\n\n\n<p>We&#8217;ll create the&nbsp;<code>build<\/code>, which will have all the compiled, static files.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run build<\/code><\/pre>\n\n\n\n<p>Finally, we&#8217;ll deploy to&nbsp;<code>gh-pages<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run deploy<\/code><\/pre>\n\n\n\n<p>And we&#8217;re done! The app is now available live at\u00a0<a href=\"https:\/\/taniarascia.github.io\/react-tutorial\">https:\/\/taniarascia.github.io\/react-tutorial<\/a>. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<h6 id=\"conclusion\">Conclusion<\/h6>\n\n\n\n<p>This article should have given you a good introduction to React, simple and class components, state, props, working with form data, pulling data in from an API, and deploying an app. There is much more to learn and do with React, but I hope you feel confident delving in and playing around with React yourself now.<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/github.com\/taniarascia\/react-tutorial\">View Source on GitHub<\/a><\/li><li><a href=\"https:\/\/taniarascia.github.io\/react-tutorial\/\">View Project<\/a><\/li><\/ul>\n\n\n\n<p>Please let me know if anything was unclear, or if there&#8217;s anything else you&#8217;d like to see in this or a subsequent article.<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/www.taniarascia.com\/static\/a314736828868b35d906cd426cbb7234\/f69df\/Screen-Shot-2018-08-19-at-6.37.09-PM.png\" target=\"_blank\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is no consensus on the right way to organize a React application. React The essential guide to getting started with React. This walkthrough tutorial will have you building an application in React from scratch, learning about state, props, and components along the way. I&#8217;ve been hearing about React since I first started learning JavaScript, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":""},"_links":{"self":[{"href":"https:\/\/www.gadgetappdevelopment.com\/blog\/wp-json\/wp\/v2\/pages\/44"}],"collection":[{"href":"https:\/\/www.gadgetappdevelopment.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.gadgetappdevelopment.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.gadgetappdevelopment.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gadgetappdevelopment.com\/blog\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":34,"href":"https:\/\/www.gadgetappdevelopment.com\/blog\/wp-json\/wp\/v2\/pages\/44\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/www.gadgetappdevelopment.com\/blog\/wp-json\/wp\/v2\/pages\/44\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/www.gadgetappdevelopment.com\/blog\/wp-json\/wp\/v2\/media?parent=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}