<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aaron Smith</title>
    <description>The latest articles on DEV Community by Aaron Smith (@aaronsm46722627).</description>
    <link>https://dev.to/aaronsm46722627</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F381811%2F6640a661-f3b8-47e2-903e-a2aeb50d5c26.jpg</url>
      <title>DEV Community: Aaron Smith</title>
      <link>https://dev.to/aaronsm46722627</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aaronsm46722627"/>
    <language>en</language>
    <item>
      <title>React Components and what in the world is props ? </title>
      <dc:creator>Aaron Smith</dc:creator>
      <pubDate>Fri, 13 Nov 2020 20:47:00 +0000</pubDate>
      <link>https://dev.to/aaronsm46722627/react-components-and-what-in-the-world-is-props-3oap</link>
      <guid>https://dev.to/aaronsm46722627/react-components-and-what-in-the-world-is-props-3oap</guid>
      <description>&lt;p&gt;In a previous article we talked about rendering in React and as a simple example rendering React elements. But this doesn’t reflect what everyone who uses React does when creating an application. In this article we will be discussing the concept of components and an important concept called props which goes over how data can flow in React.&lt;/p&gt;

&lt;p&gt;React components allow an application to be split into discrete, reusable user interfaces. A somewhat accurate analogy would be that React components are very much like JavaScript functions.&lt;/p&gt;

&lt;p&gt;React components can be either function Components or class components. Let’s deal with function components first.&lt;/p&gt;

&lt;p&gt;The easiest way to define a React function component is to write a function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This almost looks like a regular JavaScript function. This function component accepts a props’ argument. Props stand for properties, we will get to them but for now think of props as an object that carries data with it that can be used in our function component. This function component returns some JSX that accesses the props object key ‘name’.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rendering a React Component
&lt;/h2&gt;

&lt;p&gt;The way we can represent a React component in JSX is like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Welcome /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our circumstance the React component accepts a props argument. Now when we write a React component in JSX we can define what the props object will be.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Welcome name='Sarah' /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are saying we want the props object to have the key ‘name’ and the value ‘Sarah’. We call this the JSX attribute. When we define this attribute it means we are defining the prop with a key of name and value of Sarah. So now within our function component we can access this value by props.name!&lt;/p&gt;

&lt;p&gt;So knowing that, we can see how we would render this simple component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Welcome&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sarah&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are calling upon the &lt;code&gt;ReactDOM.render&lt;/code&gt; function. React recognises that this is a component. It passes the ‘attribute’ name to the component we call props. React then processes this function. This function returns JSX and this gets rendered by React and updates the DOM. This then displays the output on the screen.&lt;/p&gt;

&lt;p&gt;Note! You should always start a component with a capital letter, &lt;code&gt;&amp;lt;div /&amp;gt;&lt;/code&gt; represents an HTML tag but &lt;code&gt;&amp;lt;Div /&amp;gt;&lt;/code&gt; is interpreted as a component.&lt;/p&gt;

&lt;p&gt;Now that we understand what components are and how to render them. We need to take this one step further and see how we would conceivable construct something like a React app. We’ve already talked about the fact that components are discrete pieces of code that can split parts of a user interface.&lt;/p&gt;

&lt;p&gt;So the key to components is that we can refer to other components in their output. When we create an App, we create a function component called  that we can refer to multiple components that split the application into discrete user interfaces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Welcome&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sara&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Welcome&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Cahal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Welcome&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Edite&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have our App component, that returns three iterations of the Welcome component with different attributes. We then call upon the &lt;code&gt;ReactDOM.render()&lt;/code&gt; that renders the App component. When we call upon this function we are actually triggering React to render the three Welcome components.&lt;/p&gt;

&lt;p&gt;The beauty of this type of setup is that we can split our user interface into ever smaller and simpler components. Notice how we don’t have to have our Welcome function component inside the App component. This allows us to extract away components making the code more readable.&lt;/p&gt;

&lt;p&gt;The key to functional components and props is that props should not be modified by the function component. We call this a pure function, that does not change it’s input. However, we know that things within complex applications DO change and there is a way to deal with this possibility in React.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article we have defined what a component is and why it’s at the heart of React applications. The concept of a component means we can split a very complex application down into many small components. With a component we also have to have a way to transfer data into these components. This is where the concept of a prop comes in, because a function component acts much like a function, think of props as an object we pass as an argument much like a function. We can define the prop by the attributes of the JSX that represents the component. We saw an example of this. This means we can render multiple iterations of the same component with different data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Articles by Author
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/aaronsm46722627/why-you-should-know-about-the-virtual-dom-48bc"&gt; Why you should know about the Virtual DOM &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aaronsm46722627/why-should-you-care-about-how-the-browsers-work-in-react-13mn"&gt; Why should you care about how the Browser works in React &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aaronsm46722627/why-you-should-be-using-fragments-o5n"&gt; Why you should be using Fragments &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/aaronsm46722627/why-the-div-in-react-1cfk"&gt; Why the div in React &lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  About the Author
&lt;/h2&gt;

&lt;p&gt;I’m a practising medical physician and educationalist as well as a web developer. Please see here for further details about what I’m up to project-wise on my blog and other posts. If you want to get in contact with me, please do so here&lt;br&gt;
&lt;a href="mailto:aaron.smith.07@aberdeen.ac.uk"&gt;aaron.smith.07@aberdeen.ac.uk&lt;/a&gt; or on Twitter @aaronsmithdev.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>A Journey through React Rendering</title>
      <dc:creator>Aaron Smith</dc:creator>
      <pubDate>Thu, 12 Nov 2020 21:30:59 +0000</pubDate>
      <link>https://dev.to/aaronsm46722627/a-journey-through-react-rendering-3obn</link>
      <guid>https://dev.to/aaronsm46722627/a-journey-through-react-rendering-3obn</guid>
      <description>&lt;p&gt;In the blocks of understanding necessary to get a model of React in our minds the concept of rendering is key. We are using the syntax of JSX to design our user interface and translating this into an output on the screen, this is rendering.&lt;/p&gt;

&lt;p&gt;Now suppose in an HTML document you have &lt;code&gt;&amp;lt;div id="root&amp;gt; &amp;lt;/div&amp;gt;&lt;/code&gt;. We can use this to create our React application.&lt;/p&gt;

&lt;p&gt;We call this the root DOM node. In fact applications build solely in React you usually only have one Root DOM node.&lt;/p&gt;

&lt;p&gt;From the very simplest of building blocks of a React application we have elements, they are created in JSX which can be rendered on the page. As complexity gets higher we bundle elements into a component. That component becomes many and these components are usually held under one larger component which we inventively call ‘App’.&lt;/p&gt;

&lt;p&gt;Everything we create with React gets managed insides this ‘root’ DOM node. Before we go down the rabbit hole of complexity, first we should understand how to render an element before talking about rendering components. We will get an understanding of what we mean by React managing everything inside this &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rendering an Element
&lt;/h2&gt;

&lt;p&gt;So we know that React has to translate JSX, into something that eventually will go on the screen. We call this rendering. React has a function called &lt;code&gt;ReactDOM.render()&lt;/code&gt; which allows us to take a React element as an argument and where we want to put the code in the HTML. ReactDOM is a React library which has a bunch of functions that deal with the DOM.&lt;/p&gt;

&lt;p&gt;The ReactDOM methods are used at the top of the application. It provides a way for code to escape through and be displayed onto the page. It is the primary gateway between React and the DOM.&lt;/p&gt;

&lt;p&gt;Let’s see how this works&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByElementId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output on Screen&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we should know that a React element is immutable, it only provides a snapshot of a potential user interface at a time. You can imagine this is not really a way to make applications!&lt;/p&gt;

&lt;p&gt;Lets talk through the &lt;code&gt;render()&lt;/code&gt; function first. The parameters it takes are the following&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React element&lt;/li&gt;
&lt;li&gt;Selected DOM node to append to&lt;/li&gt;
&lt;li&gt;Callback function (optional)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This selected DOM node acts as the root of the tree structure of all react elements. It is this structure which allows for multiple components which are viewed as children of the root React Element. Below is a diagram to show this.&lt;/p&gt;

&lt;p&gt;Now it should be said that a render can be triggered by something happening inside a component as well as actually just displaying a static page on the screen. This ‘re-render’ could be a change that we want to occur on the webpage and that these re-renders can happen multiple times is the beauty of React. It allows for many changes to happen or data to be stored for eventual use without it being complicated.&lt;/p&gt;

&lt;p&gt;To get from JSX to what appears on the screen. React renders a new virtual DOM and compares this against the old virtual DOM. A process called diffing which React calculates the differences between these virtual DOM’s. This then gets compared against the actual DOM and only changes that need to made to the DOM are made. This process is called reconciliation. We won’t get too deep into this in this article but just to have a high level overview of what rendering achieves.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use render() ?
&lt;/h2&gt;

&lt;p&gt;Now we know what it does what can we glean from its use.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Single-page applications &lt;/b&gt;&lt;/p&gt;

&lt;p&gt;You’ll have no doubt heard about single-page applications. React is well suited for this type of application. We can have an almost blank page of HTML where we can direct React to append the application within a div container as we explained above. React allows us to change things on the DOM multiple times without having to refresh the page. We do this by triggering a re-render in our React Application multiple times if necessary, updating the page the way we want it to. This gives us the ability to create dynamic content on a page without having to refresh it seamlessly. To fully understand this we have to delve into the concept of state, which won’t explore here!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;ReactDOM acts as an interface between React, component Tree and the DOM. The most common function is the render() function. It acts as the go between React and the DOM.&lt;/p&gt;

&lt;p&gt;Once the React root element and tree of children components have been rendered it is the reconciliation process that handles everything related to updates to the page. If we decide to update one of the child components then only changes within that component will take place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Articles by Author
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/aaronsm46722627/why-you-should-know-about-the-virtual-dom-48bc"&gt; Why you should know about the Virtual DOM &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aaronsm46722627/why-should-you-care-about-how-the-browsers-work-in-react-13mn"&gt; Why should you care about how the Browser works in React &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aaronsm46722627/why-you-should-be-using-fragments-o5n"&gt; Why you should be using Fragments &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/aaronsm46722627/why-the-div-in-react-1cfk"&gt; Why the div in React &lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  About the Author
&lt;/h2&gt;

&lt;p&gt;I’m a practising medical physician and educationalist as well as a web developer. Please see here for further details about what I’m up to project-wise on my blog and other posts. If you want to get in contact with me, please do so here&lt;br&gt;
&lt;a href="mailto:aaron.smith.07@aberdeen.ac.uk"&gt;aaron.smith.07@aberdeen.ac.uk&lt;/a&gt; or on Twitter @aaronsmithdev.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>What in the Heck is JSX ?</title>
      <dc:creator>Aaron Smith</dc:creator>
      <pubDate>Wed, 11 Nov 2020 20:19:17 +0000</pubDate>
      <link>https://dev.to/aaronsm46722627/what-in-the-heck-is-jsx-58a3</link>
      <guid>https://dev.to/aaronsm46722627/what-in-the-heck-is-jsx-58a3</guid>
      <description>&lt;p&gt;React is a JavaScript library that uses a syntax called JSX this stands for JavaScript XML. It is a syntax much like XML/HTML that can co-exist with JavaScript code. This means we can write HTML like content and combine it with JavaScript.&lt;/p&gt;

&lt;p&gt;This syntax is intended to be used by a preprocessor like Babel which converts this syntax into JavaScript that the JavaScript engine can run.&lt;/p&gt;

&lt;p&gt;JSX is a concise HTML like structure in the same file as we write the JavaScript code. Unlike in the past, we can put HTML into JavaScript.&lt;/p&gt;

&lt;p&gt;So lets see some code, as we’ll get a better sense of this doing that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks like a cross between HTML and JavaScript. Babel is able to detect this is JSX and transform it into the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Babel takes this JSX code we give it and takes the tags and content and uses them as arguments for React.createElement function. Think of JSX as a shorthand way of calling this function &lt;code&gt;React.createElement&lt;/code&gt;. The React documentation calls it ‘syntactic sugar’ for React.createElement&lt;/p&gt;

&lt;p&gt;You can see how much easier JSX is to read, particularly when you start nesting JSX. It is not a template though! It is syntax that has to be compiled into JavaScript.&lt;/p&gt;

&lt;p&gt;For the purposes of the examples we will assume that JSX gets converted, this is sometimes called rendered by React into working DOM nodes that get displayed on the page. This just reduces the complexity down of this article to focus just on JSX.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use JSX
&lt;/h2&gt;

&lt;p&gt;JSX is not created by React, it's an extension of the ECMAScript. You can use React without JSX but here is why most don't.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Less proficient coders can get started early and understand and modify it easily. Designers are also more likely to understand it!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You leverage the power of JavaScript without having to learn a template language. But remember JSX is not a template, it’s a syntax to express tree structures of a UI component&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JSX promotes the idea of inline styles, which has been a shift from previous ways to develop websites&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  JSX Rules
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;b&gt;The first part of the JSX tag determines the type of React element. We have seen this in a simple example.&lt;/b&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;b&gt;Capitalising tags indicate that the JSX tag is referring to a React component.&lt;/b&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;b&gt;We can evaluate JavaScript within our JSX by using curly braces &lt;/b&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we were to convert this and display the output HTML, the JavaScript 1+2 would evaluate and the result would be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;b&gt;We can nest these JSX elements&lt;/b&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Here&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt; 
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
         &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Item&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;         &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Item&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React will render this into a list of items!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;b&gt;You can render a list on the page using a list of JSX expressions.&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is more complicated, don’t worry if you don’t get this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;finish doc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;submit pr&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;}
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&amp;gt;&lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we give this JSX to react, if evaluates this JavaScript within the curly brackets. In this case we use the map function to create an array of JSX. We take the todos array items and wrap a &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; tag and the output is a list of the array's items&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
     &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;finish&lt;/span&gt; &lt;span class="nx"&gt;doc&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&amp;gt;,&amp;lt;li&amp;gt;submit pr&amp;lt;/&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then JavaScript interprets the JavaScript in the curly brackets and renders the bullet pointed array items we created.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;b&gt; &lt;code&gt;false&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;true&lt;/code&gt; are valid JSX, but they don’t get rendered by React onto the page. &lt;/b&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Beware, some falsy values DO get rendered. 0 for example still gets rendered.&lt;/p&gt;

&lt;p&gt;The fact that they are valid JSX, and they do not get rendered to anything on the page means we can create situations where we can conditionally render certain JSX.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;b&gt; Based on conditions, we can tell React what specific JSX we want to render&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the moment, assume that if a tag with Capitalised First letter name a /&amp;gt; is a React component, don’t worry about knowing exactly if you’re unfamiliar with it. React builds from elements up to components as it becomes more complex, and they can be written in JSX like so below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;showHeader&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Header&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we want to display the header component if showHeader variable is true. If showHeader was false, the Header component would not be seen on the screen!&lt;/p&gt;

&lt;p&gt;This is not the end to JSX. But to understand how to properly use it and how it properly fits into the React code, we have to understand a few other concepts. Like how does React turn this JSX into something on the page.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ReactDOM.render()&lt;/code&gt; function which converts all our JSX eventually into DOM nodes. We also have to understand what components are and how to create React components. Lastly to fully utilise JSX we need to understand the concept of props. Prop stands for properties and it is React’s way to pass Data down into components. This is incredibly useful and we will get to that!&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Articles by Author
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/aaronsm46722627/why-you-should-know-about-the-virtual-dom-48bc"&gt; Why you should know about the Virtual DOM &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aaronsm46722627/why-should-you-care-about-how-the-browsers-work-in-react-13mn"&gt; Why should you care about how the Browser works in React &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aaronsm46722627/why-you-should-be-using-fragments-o5n"&gt; Why you should be using Fragments &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/aaronsm46722627/why-the-div-in-react-1cfk"&gt; Why the div in React &lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  About the Author
&lt;/h2&gt;

&lt;p&gt;I’m a practising medical physician and educationalist as well as a web developer. Please see here for further details about what I’m up to project-wise on my blog and other posts. If you want to get in contact with me, please do so here&lt;br&gt;
&lt;a href="mailto:aaron.smith.07@aberdeen.ac.uk"&gt;aaron.smith.07@aberdeen.ac.uk&lt;/a&gt; or on Twitter @aaronsmithdev.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Why you should know about the Virtual DOM</title>
      <dc:creator>Aaron Smith</dc:creator>
      <pubDate>Tue, 10 Nov 2020 22:38:28 +0000</pubDate>
      <link>https://dev.to/aaronsm46722627/why-you-should-know-about-the-virtual-dom-48bc</link>
      <guid>https://dev.to/aaronsm46722627/why-you-should-know-about-the-virtual-dom-48bc</guid>
      <description>&lt;p&gt;In a previous article, we discussed the DOM and mentioned that having an understanding of this is important to understand React. Well the reason for that is the virtual DOM.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the Virtual DOM?
&lt;/h2&gt;

&lt;p&gt;Think of the virtual DOM as a concept, there is nothing called the virtual DOM within the React codebase. It serves as a useful tool to do what React was meant for, creating user interface components that deal with state (we will briefly define state below)&lt;/p&gt;

&lt;p&gt;The virtual DOM is an in memory representation of the DOM. It has the same properties, but doesn’t have all the overhead in putting that it onto the screen like a DOM. When the DOM was first conceived the idea of frequently changing things on the page was not really a concept. However, as websites have grown in more complexity having this ability to change things frequently is a major advantage.&lt;/p&gt;

&lt;p&gt;You will see a lot written about the virtual DOM but essentially all it is, is a plain old JavaScript object. This object can be manipulated easily and frequently with ease and is why React has used it as a construct.&lt;/p&gt;

&lt;p&gt;The other aspect to the virtual DOM that comes with changing things frequently is the concept of state. Essentially state is a way of storing data in a component that can change and can be used to display something on the page that you want to be dynamic. You can imagine in a complex website there may be many things you want to change and keep a track record of. Well-to-do this through plain DOM manipulation becomes an unwieldy task. It is this that React does best, so when I talk about user interfaces that deal with state, this is what I mean.&lt;/p&gt;

&lt;p&gt;React also allows us to be declarative, that is we do not care about the internals of how React does what we want. We just want to be able to tell it what state we want the component to be in and for React to eventually manipulate the DOM accordingly. Additionally, to this, every single time a state changes, then the virtual DOM gets updated. This is how we keep track of all the states.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Abstraction of the Virtual DOM
&lt;/h2&gt;

&lt;p&gt;Let's put some code into this. This is a very stripped down version of what the virtual DOM could be like.&lt;/p&gt;

&lt;p&gt;Say we have a list component which corresponds to an unordered list. It may look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;tagName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ul&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;class&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;list&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;tagName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;class&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;list__item&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="na"&gt;textContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;List item&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So you now must be thinking well how is this virtual DOM Created? Any time we write any JSX, this gets converted to a &lt;code&gt;React.Element&lt;/code&gt; function. This returns an object and it is this object that is the part of virtual DOM. Whenever you get confused about this, think back to this.&lt;/p&gt;

&lt;h2&gt;
  
  
  How React uses the Virtual DOM
&lt;/h2&gt;

&lt;p&gt;When changes need to be made, the virtual DOM gets updated by creating a new virtual DOM with the intended changes applied.&lt;/p&gt;

&lt;p&gt;Every single time a JSX element gets rendered using the &lt;code&gt;ReactDOM.render()&lt;/code&gt; function a whole new virtual DOM is created. At first this may seem inefficient but the cost in doing this is insignificant, we are essentially creating a set of objects.&lt;/p&gt;

&lt;p&gt;Below is an example of what React does at a very basic level, here we have copied the above example of a virtual DOM&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;copy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;tagName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ul&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;class&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;list&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;tagName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;class&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;list__item&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="na"&gt;textContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;List item one&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;tagName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;class&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;list__item&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="na"&gt;textContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;List item two&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This new copy of DOM is then compared to a snapshot of the previous virtual DOM. React then produces a set of instructions tells us what needs to be changed between these two versions of the virtual DOM. This process is called diffing. React makes a decision on how efficiently to make these changes and only changes the DOM for these changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;diffs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* new version of list item one */&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;oldNode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* original version of list item one */&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="cm"&gt;/* index of element in parent's list of child nodes */&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* list item two */&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZrEVV7Qh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xzu7a97dc505ej0ukbxr.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZrEVV7Qh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xzu7a97dc505ej0ukbxr.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It essentially syncs the virtual DOM with the DOM. It also means that React only changes the DOM once. This whole process is comparing and changing the virtual DOM to the eventual DOM manipulation is called reconciliation.&lt;/p&gt;

&lt;p&gt;The beauty of this is that we as the coder do not have to care about how this occurs, React takes this burden away from us and allows us to focus on other concerns.&lt;/p&gt;

&lt;p&gt;So now you should have a good grasp on the fundamentals of the virtual DOM and how react works from a high structure. Until next time!&lt;/p&gt;

&lt;h2&gt;
  
  
  About the Author
&lt;/h2&gt;

&lt;p&gt;I’m a practising medical physician and educationalist as well as a web developer. Please see &lt;a href="www.coding-medic.com"&gt; here &lt;/a&gt; for further details about what I’m up to project-wise on my blog and other posts. If you want to get in contact with me, please do so here&lt;br&gt;
&lt;a href="mailto:aaron.smith.07@aberdeen.ac.uk"&gt;aaron.smith.07@aberdeen.ac.uk&lt;/a&gt; or on Twitter @aaronsmithdev.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Why should you care about how the Browser works in React</title>
      <dc:creator>Aaron Smith</dc:creator>
      <pubDate>Sun, 08 Nov 2020 22:47:26 +0000</pubDate>
      <link>https://dev.to/aaronsm46722627/why-should-you-care-about-how-the-browsers-work-in-react-13mn</link>
      <guid>https://dev.to/aaronsm46722627/why-should-you-care-about-how-the-browsers-work-in-react-13mn</guid>
      <description>&lt;p&gt;The first time you read about React, the concept of the virtual DOM (Document Object Model) appears. Don’t worry if you haven’t come across this term! To begin to understand what the virtual DOM is, the first important step is to have an overview of the DOM first and then why React uses the virtual DOM instead. Here in this article we will look through the DOM to enhance why you’d want to know about this for React.&lt;/p&gt;

&lt;p&gt;At its simplest the DOM provides an in memory structure for the web browser to communicate the structure of the page that displays on the screen.&lt;/p&gt;

&lt;p&gt;When you start creating websites using HTML the browser has to be able to interpret the code you write. The browser does by creating a hierarchy of HTML tags, this hierarchy is expressed as a tree. The creation of that tree is done by the browser’s rendering engine. It does this by converting each HTML tag into an object called a node.&lt;/p&gt;

&lt;p&gt;All these nodes make up a tree and the root of this tree is called the Document object. This whole tree is called the Document Object Model or DOM for short.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;First page&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello, world!&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;First Page of Blog&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is what the DOM representation would look like&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6mpe8khirfino27mlku2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6mpe8khirfino27mlku2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each DOM node has a type, which is important to differentiate one node from another. It also has unique properties and methods that are unique to the node. Below is a list of common node types, this is not exhaustive list!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Document nodes – To specify the whole HTML document,&lt;/li&gt;
&lt;li&gt;Element nodes – To access each HTML tag&lt;/li&gt;
&lt;li&gt;Text nodes – To access the text within the HTML document&lt;/li&gt;
&lt;li&gt;DocumentFragment – A lightweight DOM held in browser memory whilst the website is being viewed. It provides a way to update the DOM in real time (Does this sound familiar?)&lt;/li&gt;
&lt;li&gt;DocumentType – Declares that the document present to the browser isHTML (&amp;lt;!DOCTYPE html&amp;gt; )&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now to clarify up some points you may have heard about the DOM&lt;/p&gt;

&lt;h2&gt;
  
  
  The DOM is NOT what you see on the browser
&lt;/h2&gt;

&lt;p&gt;What you see on the browser is a combination of the DOM and representation of CSS! This is called the render tree. The difference between a render tree and the DOM is that the DOM will not exclude elements in the HTML which are hidden visually.&lt;/p&gt;

&lt;p&gt;An example would be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;First page&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello, world!&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"display: none&amp;gt; Not displayed &amp;lt;/p&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The DOM representation&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frm2gx9loqmll62c6g7wu.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frm2gx9loqmll62c6g7wu.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The DOM is not the source HTML document
&lt;/h2&gt;

&lt;p&gt;The DOM actually corrects some mistakes, if say we forget to add a body tag into our HTML.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;
    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="na"&gt;h1&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The DOM representation&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4ql3doolu2ofkap0gxiw.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4ql3doolu2ofkap0gxiw.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how a body element has been inserted into the DOM ? HTML is forgiving! &lt;/p&gt;

&lt;h2&gt;
  
  
  The DOM and JavaScript
&lt;/h2&gt;

&lt;p&gt;What has this got to do with React or JavaScript for that matter ?&lt;/p&gt;

&lt;p&gt;Well it is possible to create and add DOM nodes using JavaScript. The relationship to JavaScript, the purpose of the DOM is to provide an interface for JavaScript to be able to change it (add nodes, remove nodes, replacing, adding events). It is this DOM manipulation that gives JavaScript its ability to provide the dynamic functionality we see on websites all the time. Each one of these DOM nodes will have properties and methods unique to it which can be used and manipulated.&lt;/p&gt;

&lt;p&gt;A question that might ask about the DOM with regard to its interaction with JavaScript is what happens when the browser encounters   ?&lt;/p&gt;

&lt;p&gt;Well the browser stops creating the DOM, it blocks any further creation and executes the script we have written. Once the script has been run, then the rest of the DOM then gets created.&lt;/p&gt;

&lt;p&gt;So now we understand at a high level the DOM, what has this got to do with React ? Well React uses something called the virtual DOM that interacts with the DOM on a need only basis.&lt;/p&gt;

&lt;p&gt;Well to answer this fully, we have to think about why would you use this in React instead of vanilla JavaScript? (Hint it is not because the DOM is slow!). We will get into this another article.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Why you should be using Fragments</title>
      <dc:creator>Aaron Smith</dc:creator>
      <pubDate>Fri, 06 Nov 2020 21:43:33 +0000</pubDate>
      <link>https://dev.to/aaronsm46722627/why-you-should-be-using-fragments-o5n</link>
      <guid>https://dev.to/aaronsm46722627/why-you-should-be-using-fragments-o5n</guid>
      <description>&lt;p&gt;In the last blog post (see &lt;a href="https://dev.to/aaronsm46722627/why-the-div-in-react-1cfk"&gt; here&lt;/a&gt;) we looked at why need to wrap our React elements in a div, please see here if you haven't already read that post! &lt;/p&gt;

&lt;p&gt;At the end of that post we discussed why having unnecessary div elements entering the DOM could potentially cause problems. Well I guess there must have been a solution to this right ?&lt;/p&gt;

&lt;p&gt;In the React release 16.2, a new feature was introduced called a React fragment. Think of this as a component that doesn't translate to HTML. &lt;/p&gt;

&lt;p&gt;Instead of using &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;'s to wrap our elements we use   or the short hand syntax &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Here below we have the typical solution to error you get when you don't wrap your React elements in a &lt;code&gt;div&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
           &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;           &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's how we'd rewrite this using &lt;code&gt;React.fragment&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; 
           &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fragment&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
             &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;             &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;           &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/React.fragment&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now writing &lt;code&gt;React.fragment&lt;/code&gt; can be a pain! So React also introduced the &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt; syntax&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; 
           &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
             &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;             &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;           &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;React.fragment&lt;/code&gt; gets converted into regular JavaScript by a transpiler and after conversion looks like this. Something we've seen in a previous post! &lt;/p&gt;

&lt;p&gt;&lt;code&gt;React.createElement(React.fragment, null, ...children)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The difference is, it does not get inserted into the DOM! &lt;/p&gt;

&lt;h2&gt;
  
  
  What are the advantages to Fragments
&lt;/h2&gt;

&lt;p&gt;As explained in a previous article. Here are the main reasons why you'd use fragments.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Your react application is getting bigger and the number of useless divs in the DOM are mounting up.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The performance is slightly better and so if you are wanting to shave some time off then this may be a way to do it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you are concerned with layout and the rendering of the eventual HTML does not compute with how the layout should appear&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How can I use them
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Returning groups of Elements
&lt;/h3&gt;

&lt;p&gt;This one is taken straight from the React docs. Suppose we want to render a table using React. Well wrapping the &lt;code&gt;td&lt;/code&gt; tags in a &lt;code&gt;div&lt;/code&gt; will not render the table correctly! However using React fragment does!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Fragment&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; 
           &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fragment&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
               &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;td&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/td&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;               &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;td&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/td&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;           &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/React.Fragment&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This renders to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;table&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;tr&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;Hello&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;World&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/table&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Conditionally Rendering
&lt;/h3&gt;

&lt;p&gt;Here we are rendering a form that either tells us if the user has logged in or not. If not, we use Fragments to display multiple React Elements that display the form to login. If it is true then we display a logged in message. Note we are using the ternary operator to do this type of conditional rendering!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; 
           &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
             &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;LoggedIn&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Fragment&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h3&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h3&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;You&lt;/span&gt; &lt;span class="nx"&gt;are&lt;/span&gt; &lt;span class="nx"&gt;logged&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/React.Fragment&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Fragment&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h3&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Login&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h3&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;username&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Username&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&amp;gt;&amp;lt;br/&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;username&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;br&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Password&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&amp;gt;&amp;lt;br/&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;br&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/React.Fragment&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;)}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Displaying Arrays with Fragments
&lt;/h3&gt;

&lt;p&gt;Now sometimes you will want to display an array but in order to do this, React recommends you include a key property. This is because it makes it easier for react to change the DOM based on this. Now do note that you should use &lt;code&gt;React.fragment&lt;/code&gt; rather than &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;, as of now &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt; does not support the use of a key property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="na"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;123456789&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;234567890&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;345678901&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Fragment&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Fragment&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/React.Fragment&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;))}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/React.Fragment&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  About the Author
&lt;/h1&gt;

&lt;p&gt;I'm a practising medical physician and educationalist as well as a web developer.&lt;/p&gt;

&lt;p&gt;Please see &lt;a href="www.coding-medic.com"&gt;here&lt;/a&gt; for further details about what I'm up to project-wise on my blog and other posts.&lt;/p&gt;

&lt;p&gt;I'd be grateful for any comments or if you want to collaborate or need help with python please do get in touch. If you want to get in contact with me, please do so here&lt;br&gt;
&lt;a href="mailto:aaron.smith.07@aberdeen.ac.uk"&gt;aaron.smith.07@aberdeen.ac.uk&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>programming</category>
    </item>
    <item>
      <title>Why the Div in React ?</title>
      <dc:creator>Aaron Smith</dc:creator>
      <pubDate>Thu, 05 Nov 2020 20:38:59 +0000</pubDate>
      <link>https://dev.to/aaronsm46722627/why-the-div-in-react-1cfk</link>
      <guid>https://dev.to/aaronsm46722627/why-the-div-in-react-1cfk</guid>
      <description>&lt;p&gt;So when you first start using react, you will have no doubt written something like the below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;     &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's wrong with that you might ask ?&lt;/p&gt;

&lt;p&gt;React tries to convert our JSX and this pops out&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nx"&gt;Failed&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
 &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
&lt;span class="nx"&gt;Syntax&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Adjacent&lt;/span&gt; &lt;span class="nx"&gt;JSX&lt;/span&gt; &lt;span class="nx"&gt;elements&lt;/span&gt; &lt;span class="nx"&gt;must&lt;/span&gt; &lt;span class="nx"&gt;be&lt;/span&gt; &lt;span class="nx"&gt;wrapped&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
&lt;span class="nx"&gt;an&lt;/span&gt; &lt;span class="nx"&gt;enclosing&lt;/span&gt; &lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;﻿&lt;br&gt;
Behind the scenes for all the JSX statement you write in a component, they need to be wrapped into one container. But wait why is it necessary ? &lt;/p&gt;

&lt;p&gt;To answer this we have to think about what happens when React turns our JSX into eventually the HTML we see on the page. The first step in this process is to convert any JSX statement into an object. Behind the scenes React takes our JSX and a transpiler like Babel feeds the parts of that JSX into the &lt;code&gt;React.createElement&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;If we look at the API for createElement&lt;/p&gt;

&lt;p&gt;&lt;code&gt;function createElement(elementType, props, […children]) {}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The arguments are defined as&lt;/p&gt;

&lt;p&gt;1) &lt;code&gt;elementType&lt;/code&gt; - The HTML Tag you want to display&lt;/p&gt;

&lt;p&gt;2) &lt;code&gt;props&lt;/code&gt; - Data like attributes or styling you want to pass&lt;/p&gt;

&lt;p&gt;3) &lt;code&gt;children&lt;/code&gt; - Anything you want to pass between the eventual HTML tags, most likely some text but can be other things (see below!)&lt;/p&gt;

&lt;p&gt;Here's the example of our JSX from above&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&amp;gt; /&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;JSX&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Becomes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React.createElement(&amp;lt;p&amp;gt;, null, 'Hello')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;React.createElement&lt;/code&gt; function takes only one 'elementType' , that is the &lt;/p&gt;
&lt;p&gt; part of our JSX element we want to eventually display. &lt;/p&gt;

&lt;p&gt;We can't have two JSX statements that will individually have separate &lt;code&gt;React.createElement&lt;/code&gt; functions. This means two return statements and React will throw an error! Each function must only return one value in JavaScript.&lt;/p&gt;

&lt;p&gt;So what is the solution to this conundrum? &lt;/p&gt;

&lt;p&gt;We wrap our JSX into a div.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; 
       &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
         &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;         &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;     &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So behind the scenes this is what it looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="nx"&gt;Return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
     &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;null&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;null&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;null&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The App function can only return one value, by making our JSX statement a child of a div we can assign as many &lt;code&gt;React.createElement&lt;/code&gt;'s as we like to get the output what we want.&lt;/p&gt;

&lt;p&gt;Now the problem here with wrapping inside a div is that we end up bloating the DOM up with pointless div sitting in our eventual HTML. This may not be a problem for a simple component, but more complex components you can imagine how this affects the eventual running of the component and App. &lt;/p&gt;

&lt;p&gt;The other problem you may come into contact with is layout in your React application. You can imagine if there are div elements where there shouldn't be, using FlexBox to layout your page may not look the way you intended.&lt;/p&gt;

&lt;p&gt;To deal with this problem, we will see in the next article!&lt;/p&gt;

&lt;h2&gt;
  
  
  About the Author
&lt;/h2&gt;

&lt;p&gt;I'm a practising medical physician and educationalist as well as a web developer.&lt;/p&gt;

&lt;p&gt;Please see &lt;a href="http://www.coding-medic.com"&gt;here&lt;/a&gt; for further details about what I'm up to project-wise on my blog and other posts.&lt;/p&gt;

&lt;p&gt;I'd be grateful for any comments or if you want to collaborate or need help with python please do get in touch. If you want to get in contact with me, please do so here &lt;br&gt;
&lt;a href="mailto:aaron.smith.07@aberdeen.ac.uk"&gt;aaron.smith.07@aberdeen.ac.uk&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Embedded QR Code Scanner and Browser in React Native</title>
      <dc:creator>Aaron Smith</dc:creator>
      <pubDate>Sun, 01 Nov 2020 20:49:45 +0000</pubDate>
      <link>https://dev.to/aaronsm46722627/embedded-qr-code-scanner-and-browser-in-react-native-5752</link>
      <guid>https://dev.to/aaronsm46722627/embedded-qr-code-scanner-and-browser-in-react-native-5752</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;For a recent freelancing project I was asked to provide QR Code scanning functionality embedded in a stand-alone mobile iOS app. I was aware of QR Code scanning packages available on the node package manager but never really needed to use them in my work! &lt;/p&gt;

&lt;p&gt;Some searching on blogs and packages' documentation showed you how to read a QR Code and display the information, but not actually DO anything with that information. This was relatively frustrating given that the point of a QR code is to transfer information and usually in the form of a link to a browser. &lt;/p&gt;

&lt;p&gt;In this tutorial, I will take you through show you the steps I took to create an embedded QR Code Scanner for my React Native iOS app. By the end of this tutorial, you will have a good understanding of the parts of React that are relevant to React Native, including state, the lifecycle of a component.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you will learn 
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;The difference between React class components and function components&lt;/li&gt;
&lt;li&gt;How to create a React class component&lt;/li&gt;
&lt;li&gt;How to update the state in a React class component&lt;/li&gt;
&lt;li&gt;How to use conditional rendering to toggle between camera and browser in our code. &lt;/li&gt;
&lt;li&gt;About the different React Native built-in core components&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Setting up 
&lt;/h2&gt;

&lt;p&gt;I will assume that you are familiar with React Native in setting up a fresh installation. If you are new to React Native then please see here to get set up before continuing with this tutorial!&lt;/p&gt;

&lt;p&gt;We must install the following packages &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;QR Code Scanner&lt;/li&gt;
&lt;li&gt;React Native Web View&lt;/li&gt;
&lt;li&gt;React Native Camera&lt;/li&gt;
&lt;li&gt;React Native Permissions&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After a fresh installation of React Native, within the app directory in the terminal type the following commands&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save react-native-camera
npm install --save react-native-webview
npm install --save react-native-qrcode-scanner
npm install --save react-native-permissions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now don't forget to&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ios &amp;amp;&amp;amp; pod install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Setting up the QR Code Scanner imports
&lt;/h2&gt;

&lt;p&gt;Before we start, please make sure you're creating a blind React Native app and using Xcode. This is because we want to be able to allow permission for our app to access the camera which we can do by altering the settings in Xcode.&lt;/p&gt;

&lt;p&gt;Let's first import all the packages&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Notes
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We are importing extending the Component class from React in the first line. This is required to create a React class component.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We require some built-in core React Native components. View which is a container that supports layout. Text for displaying text and TouchableOpacity for a button we will create. StyleSheet is necessary to create basic styling choices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We import another React Native component called WebView as a way to display the browser which we can use for our QR Code scanner.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We import the QR Code Scanner and RNCamera. RNCamera is what is used by the QR Code scanner package so it's important to import this as well&lt;br&gt;
As part the setup please do follow the starting instructions for the QR Code Scanner docs here for the QR Code scanner app. You will need to make sure that you allow your app to have permission to access the camera on your phone for the app to work.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Understanding and Creating the React Class Component
&lt;/h2&gt;

&lt;p&gt;To create this app we are going to use a React class component for our code. This is because class components are useful to modify something called state. You may ask what is state? &lt;/p&gt;

&lt;p&gt;Well, state is an object that we create that can change the values of and this changes how React component renders. If we change the state value, this re-renders the component to provides a change to the output we see on the screen.&lt;/p&gt;

&lt;p&gt;An analogy may help us here to cement what this all means. What is the difference between water and ice? The temperature of course! That number is a measurement of thermal energy. Changing that number changes water into ice and vice versa. This is exactly what happens in changing the state of a React Component. The output of the React Component will change depending on a change in the state object. &lt;/p&gt;

&lt;p&gt;We are going to use this concept of state in order to toggle between the QR Code reader and the browser by changing the state object. This is why we want to use a class component!&lt;/p&gt;

&lt;p&gt;Class components provide the functionality to change the state object whereas function components are called stateless. This is the fundamental difference between class React components and function React components. Keep this in mind when you're writing your React and React Native applications. &lt;/p&gt;

&lt;p&gt;With that explanation out of the way Let's have a look at the first few lines of the code of our App.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;So to create a React class component we must first extend the React Component. The React component is the simplest building block of React and is necessary to refer to it in any class component we create. &lt;/p&gt;

&lt;p&gt;The state object with two keys &lt;code&gt;webview&lt;/code&gt; and &lt;code&gt;url&lt;/code&gt;. We set them to &lt;code&gt;false&lt;/code&gt; and &lt;code&gt;''&lt;/code&gt; respectively. The reason we say the key &lt;code&gt;webview&lt;/code&gt; is false is that we don't want the &lt;code&gt;WebView&lt;/code&gt; component to render first. Also, we don't currently yet know what the URL of the QR code is going to be. To direct the &lt;code&gt;WebView&lt;/code&gt; component to the correct URL, we need to some be able to how to change that URL when the QR code scans.&lt;/p&gt;

&lt;h2&gt;
  
  
  The QR Code Scanner 
&lt;/h2&gt;

&lt;p&gt;So within the class, we now are ready to go through the code for the QR Code Scanner.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;First the &lt;code&gt;render(){}&lt;/code&gt; is called. This is the only necessary method for a class component. This is the method that will render our component to do something on a webpage or in our case, the mobile app.&lt;/p&gt;

&lt;p&gt;We wrap our logic within a &lt;code&gt;View&lt;/code&gt; built-in core component of React Native. This is the equivalent of div HTML tag on rendering. They can be styled, in this case, we are styling the component to fill the full screen using &lt;code&gt;flex:1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, we are providing two pieces of JavaScript within curly braces inside the View component.&lt;/p&gt;

&lt;p&gt;We can access the state object key &lt;code&gt;webview&lt;/code&gt; by &lt;code&gt;this.state.webview&lt;/code&gt;, we know this to be false at this stage. After this expression, you will first notice that the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator is used. This is because we are using conditional rendering. That is we want to render the component depending on two expressions truthiness or falsiness. It is this that allows us to switch between a browser and the QR code scanner.&lt;/p&gt;

&lt;p&gt;The logic goes like this, we defined the state key &lt;code&gt;webview&lt;/code&gt; with a false value. So with the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator we get a short-circuiting evaluation of the two subexpressions, that is the whole statement is false.&lt;/p&gt;

&lt;p&gt;Now The &lt;code&gt;&amp;lt;WebView&amp;gt;&lt;/code&gt; built-in core component is how we tell React Native we want a browser to appear on the screen. The arguments &lt;code&gt;source={{uri: this.state.url}&lt;/code&gt; is how we define what URL we want to serve to the browser. We give the component a style and scale it to fit the page.&lt;/p&gt;

&lt;p&gt;So in this piece of JavaScript, the expression is false. That is, we don't want React Native to render the Webview component. That is until we change the webview state. &lt;/p&gt;

&lt;p&gt;In the second piece of JavaScript, we are asking whether the webview variable is false and if the right-hand subexpression is true. The right-hand subexpression returns true, much like any string apart from an empty string gets coerced to true in JavaScript. So the whole expression is true and the QR Code Scanner, therefore, gets rendered.&lt;/p&gt;

&lt;p&gt;The QR Code Scanner component has the following arguments. The &lt;code&gt;onread&lt;/code&gt; is necessary to provide a way to transfer data when the QR code is read. In this case, we are calling upon a method to be defined, which we are binding that is to allow us to access the method in the class outside the method itself. This is necessary to allow us to use the method as part of the QR Code component.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;reactivate&lt;/code&gt; and &lt;code&gt;reactivateTimeout&lt;/code&gt; arguments are set accordingly to make sure that the QR code scanner will be able to scan again after the first time it does. We will make use of this in the next section!&lt;/p&gt;

&lt;p&gt;So on default rendering of this component, it is the QR code scanner that should appear. &lt;/p&gt;

&lt;p&gt;Now, what is this function we want to be able to do something with when the QR code scanner reads the QR code?&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Instead of showing the URL on the screen, we want to set the state variable URL to that corresponding URL when the QR code is scanned. &lt;/p&gt;

&lt;p&gt;We do this by calling upon the &lt;code&gt;setState&lt;/code&gt;. This allows us to change our state we defined in the constructor method. The &lt;code&gt;setState&lt;/code&gt; accepts an object of keys that were defined on the state, but this time we are changing the values of those keys. Firstly we are grabbing the data from the QR code that's been read, this is bundled in the variable &lt;code&gt;e.data&lt;/code&gt; and setting the URL key to that. We are then changing &lt;code&gt;webview&lt;/code&gt; to true.&lt;/p&gt;

&lt;p&gt;By using &lt;code&gt;setState&lt;/code&gt; the component automatically gets a re-render with the updated state. We are telling React we want to re-render the component. This time the first piece of JavaScript in curly brackets used in our conditional rendering is now true instead of the second piece of JavaScript in curly brackets. The browser now loads with the URL from the QR code!&lt;/p&gt;

&lt;p&gt;So with those essentials out of the way, you might be thinking well what is next? Well think about it from a user perspective, we now can browse the website inside our app, but what if we have another QR code we want to scan? We want a way to go back to the QR code scanner.&lt;/p&gt;

&lt;h2&gt;
  
  
  Toggling back to QR Code Scanner
&lt;/h2&gt;

&lt;p&gt;So we may have guessed how we toggle back by now. By modifying the state of course! In this case, we want to update the state such that &lt;code&gt;webview&lt;/code&gt; is false. That way the QR code scanner gets rendered instead when we re-render the component&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here we provide a button that we can click, with some styling. Essentially we are centring the content by &lt;code&gt;alignItems&lt;/code&gt; and &lt;code&gt;justifyContent&lt;/code&gt;. We are making the button a circle which has some opacity to it. We have then positioned it absolutely in the bottom right-hand corner of the screen. If you're familiar with CSS then this should seem pretty familiar to you!&lt;/p&gt;

&lt;p&gt;Now with the button, we can use the argument &lt;code&gt;onPress&lt;/code&gt; to pass a function or a function call when we press the button. In this case, we're providing a function that updates the state of webview to false. This then re-renders the React component and this is how we toggle the QR code scanner to appear again. &lt;/p&gt;

&lt;p&gt;The Text built-in component allows us to communicate what the button does, and we make sure that the text is aligned in the centre. &lt;/p&gt;

&lt;p&gt;Now we have a fully functioning QR code scanner with an embedded browser!&lt;/p&gt;

&lt;p&gt;If you'd like to see the full code example please see &lt;a href="https://gist.github.com/medic-code/f95004863480bc6552fab53c8fe7fc6e"&gt; here &lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Preview
&lt;/h2&gt;

&lt;p&gt;Please see &lt;a href="https://coding-medic.com/example/"&gt;here&lt;/a&gt; for a preview of the above code. &lt;/p&gt;

&lt;h2&gt;
  
  
  Questions to Check your understanding
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;What is a React component? &lt;/li&gt;
&lt;li&gt;Why would you use a class component instead of a function &lt;/li&gt;
&lt;li&gt;component in React Native? &lt;/li&gt;
&lt;li&gt;What is state? &lt;/li&gt;
&lt;li&gt;What is conditional rendering? &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this tutorial, you have learned the rationale for using class components. We have also covered how using the concepts of state and conditional rendering can be used to dynamically change a screen on your mobile app. In this example, we have shown how you can toggle between a QR code reader and a browser on the same screen, and toggle back to the camera once you're done browsing! How cool is that? &lt;/p&gt;

&lt;p&gt;Until next time! Please do check out my other articles &lt;/p&gt;

&lt;h1&gt;
  
  
  About the Author
&lt;/h1&gt;

&lt;p&gt;I'm a practising medical physician and educationalist as well as a web developer.&lt;/p&gt;

&lt;p&gt;Please see &lt;a href="www.coding-medic.com"&gt;here&lt;/a&gt; for further details about what I'm up to project-wise on my blog and other posts. &lt;/p&gt;

&lt;p&gt;I'd be grateful for any comments or if you want to collaborate or need help with python please do get in touch. If you want to get in contact with me, please do so here &lt;a href="mailto:aaron.smith.07@aberdeen.ac.uk"&gt;aaron.smith.07@aberdeen.ac.uk&lt;/a&gt; or on twitter at @aaronsmithdev&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>react</category>
      <category>javascript</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Using Scrapy from a single Python script</title>
      <dc:creator>Aaron Smith</dc:creator>
      <pubDate>Wed, 27 May 2020 20:34:22 +0000</pubDate>
      <link>https://dev.to/aaronsm46722627/using-scrapy-from-a-single-python-script-1jdk</link>
      <guid>https://dev.to/aaronsm46722627/using-scrapy-from-a-single-python-script-1jdk</guid>
      <description>&lt;p&gt;Scrapy is a great framework to use for scraping projects. However did you know there is a way to run Scrapy straight from a script? In fact, looking at the documentation, there are two ways to run Scrapy. Using the Scrapy API or the framework. &lt;/p&gt;

&lt;p&gt; Here we will discuss using the Scrapy API, to access the require settings and classes needed to run scrapy in a single python script. This is an area touched on only briefly within the documentation and the main reason for why a tutorial is worth discussing the more practical aspects of writing python scripts. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;h3&gt;&lt;u&gt;In this article you will learn&lt;/u&gt;&lt;/h3&gt;&lt;/b&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Why you would use scrapy from a script&lt;/li&gt;
&lt;li&gt;Understand the basic script every time you want access scrapy from an individual script &lt;/li&gt;
&lt;li&gt;Understand how to specify customised scrapy settings &lt;/li&gt;
&lt;li&gt; Understand how to specify HTTP requests for scrapy to invoke &lt;/li&gt;
&lt;li&gt; Understand how to process those HTTP responses using scrapy under an one script. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;&lt;u&gt;&lt;b&gt;Why Use Scrapy from a Script ? &lt;/b&gt;&lt;/u&gt;&lt;/h3&gt;

&lt;p&gt;Scrapy can be used for a heavy duty scraping work, however there are a lot of projects that are actually quite small and don't require the need for using the whole scrapy framework. This is where using scrapy in a python script comes in. No need to use the whole framework you can do it all from a python script.&lt;br&gt;
The Scrapy API allows you to run scrapy entirely within one script. It uses only one process per spider. &lt;/p&gt;

&lt;p&gt;Lets see what the basics of this look like before fleshing out some of the necessary settings to scrape.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;h3&gt;&lt;u&gt;Basic Script &lt;/u&gt;&lt;/h3&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;The key to running scrapy in a python script is the &lt;code&gt;CrawlerProcess&lt;/code&gt; class. This is a class of the Crawler module. It provides the engine to run scrapy within a python script. Within the &lt;code&gt;CrawlerProcess&lt;/code&gt; class, python's twisted framework is imported. &lt;/p&gt;

&lt;p&gt;Twisted is a python framework that is used for input and output processes like http requests for example. Now it does this through what's called a twister event reactor. Scrapy is actually built on top of twisted! We wont go into too much detail here but needless to say, the &lt;code&gt;CrawlerProcess&lt;/code&gt; class imports a twisted reactor which listens for events like an multiple http requests. This is at the heart of how scrapy works.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CrawlerProcess&lt;/code&gt; assumes that a twisted reactor is NOT used by anything else, like for example another spider. With that lets look at the the code below.&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import scrapy
from scrapy.crawler import CrawlerProcess
class TestSpider(scrapy.Spider):
    name = 'test'
if __name__ == "__main__":
  process = CrawlerProcess()
  process.crawl(TestSpider)
  process.start()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt; To use Scrapy We must create a our own spider, this is done by creating a class which inherits from &lt;code&gt;scrapy.Spider&lt;/code&gt; class. &lt;code&gt;scrapy.Spider &lt;/code&gt; is the most basic spider that we must derive from in all scrapy projects.  &lt;/li&gt; 
&lt;li&gt;We have to give this spider a name for it to run. Spiders will require a couple of functions and an URL to scrape but for this example we will omit this for the moment. &lt;/li&gt;

&lt;li&gt;Now you see &lt;code&gt;if __name__ == "__main__"&lt;/code&gt;. This is used as a best practice in python. When we write a script you want to it to be able to run the code but also be able to import that code somewhere else. &lt;/li&gt; 

&lt;li&gt;We instantiate the class &lt;code&gt;CrawlerProcess&lt;/code&gt; first to get access to the functions we need to start scraping data. &lt;code&gt;CrawlerProcess&lt;/code&gt; has two functions we are interested in, crawl and start
We use crawl to start the spider we created. We then use the start function to start a twisted reactor, the engine that processes and listens to our http requests we want. &lt;/li&gt; 
&lt;/ol&gt;


&lt;h3&gt;&lt;u&gt;&lt;b&gt;Adding in Settings&lt;/b&gt;&lt;/u&gt;&lt;/h3&gt;

&lt;p&gt;The scrapy framework provides a list of settings that it will use automatically, however for working with the Scrapy API we have to provide the settings explicility. The settings we define is how we can customise our spiders. The &lt;code&gt;spider.Spider&lt;/code&gt; class has a variable called &lt;code&gt;custom_settings&lt;/code&gt;. Now this variable can be used to override the settings scrapy automatically uses. We have to create a dictionary of our own settings to do this ascustom_settings variable is set to none using scrapy.&lt;/p&gt;

&lt;p&gt;You may want to use some or most of the settings scrapy provides, in which case you could copy them from there. Alternatively a list of the built-in settings can be found here.&lt;/p&gt;

&lt;p&gt;As an example see below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class TestSpider(scrapy.Spider):
    name = 'test'
    custom_settings = { 'DOWNLOD_DELAY': 1 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h3&gt;&lt;b&gt;&lt;u&gt;Specifying URLs to scrape&lt;/u&gt;&lt;/b&gt;&lt;/h3&gt;

&lt;p&gt;We have shown how to create a spider and define the settings, but we haven't actually specified any URLs to scrape, or how we want to specify the requests to the website we want to get data from. For example, parameters, headers and user-agents.&lt;br&gt;
When we create spider we also start a method called &lt;code&gt;start_requests()&lt;/code&gt;. This will create the requests for any URL we want. Now there are two ways to use this method.&lt;/p&gt;

&lt;p&gt;1)By defining the &lt;code&gt;start_urls&lt;/code&gt; attribute &lt;br&gt;
2)We implement our own function called &lt;code&gt;start_requests&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The shortest way is by defining &lt;code&gt;start_urls&lt;/code&gt;. We define it as a list of URLs we want to get. By specifying this variable we automatically use &lt;code&gt;start_requests()&lt;/code&gt;to go through each one of our URLs.&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class TestSpider(scrapy.Spider):
    name = 'test'
    custom_settings = { 'DOWNLOD_DELAY': '1' }
    start_urls = ['URL1','URL2']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;However notice how if we do this, we can't specify our own headers, parameters or anything else we want to go along with the request ? This is where implementing our own &lt;code&gt;start_requests&lt;/code&gt; method comes in. &lt;/p&gt;

&lt;p&gt;First we define our variables we want to go along with the request. We then implement our own &lt;code&gt;start_requests&lt;/code&gt; method so we can make use of the headers and params we want, as well as where we want to response to go.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class TestSpider(scrapy.Spider):
    name = 'test'
    custom_settings = { 'DOWNLOD_DELAY': 1 }
    headers = {} 
    params = {}
    def start_requests(self):
        yield scrapy.Requests(url, headers=headers, params=params)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we access the Requests method which when given an url will make the HTTP requests and return a response defined as response variable.&lt;/p&gt;

&lt;p&gt;You will notice how we didn't specific a callback ? That is we didn't specify where scrapy should send the response to the requests we just told it to get for us. &lt;/p&gt;

&lt;p&gt;Lets fix that, by default scrapy expects the callback method to be parse but it could be anything we want it to be.&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class TestSpider(scrapy.Spider):
    name = 'test'
    custom_settings = { 'DOWNLOD_DELAY': 1 }
    headers = {} 
    params = {}
    def start_requests(self):
        yield scrapy.Requests(url, headers=headers, params=params,callback = self.parse)
   def parse(self,response):
       print(response.body)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have defined the method parse which accepts a response variable, remember this is created when we ask scrapy to do the HTTP requests. We then ask scrapy to print the response body. &lt;/p&gt;

&lt;p&gt;With that, we now have the basics of running scrapy in a python script. We can use all the same methods but we just have to do a bit of configuring before hand.&lt;/p&gt;

&lt;p&gt;It's important to review content you read, the focus should be to recall and understand. I encourage you to do the exercises&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;h3&gt;&lt;b&gt;Exercises&lt;/b&gt;&lt;/h3&gt;&lt;/u&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Recall how to write a Basic Script that will enable Scrapy &lt;/li&gt;
&lt;li&gt;What is imported when we instantiate CrawlerProcess ?&lt;/li&gt;
&lt;li&gt;How do you add in your own custom settings ? &lt;/li&gt;
&lt;li&gt;Why should you use &lt;code&gt; start_requests() &lt;/code&gt; rather than &lt;code&gt; start_urls &lt;/code&gt; when directing HTTP requests with Scrapy ? &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>scrapy</category>
      <category>webscraping</category>
    </item>
  </channel>
</rss>
