<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ JSX - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ JSX - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Wed, 24 Jun 2026 17:36:25 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/jsx/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ HTML vs JSX – What's the Difference? ]]>
                </title>
                <description>
                    <![CDATA[ HTML vs JSX Hypertext Markup Language (HTML) is the standard language for documents that determine the structure of a web page.  HTML is a very important language in web development. Your code is either in HTML originally or compiles to it so browser... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/html-vs-jsx-whats-the-difference/</link>
                <guid isPermaLink="false">66adf181febac312b73075c6</guid>
                
                    <category>
                        <![CDATA[ beginners guide ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JSX ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kolade Chris ]]>
                </dc:creator>
                <pubDate>Mon, 17 May 2021 21:11:58 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/05/---.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <h2 id="heading-html-vs-jsx">HTML vs JSX</h2>
<p>Hypertext Markup Language (HTML) is the standard language for documents that determine the structure of a web page. </p>
<p>HTML is a very important language in web development. Your code is either in HTML originally or compiles to it so browsers can read it.</p>
<p>JSX, on the other hand, means JavaScript Syntax Extension or JavaScript XML as some like to put it. </p>
<p>It was created as a syntactic sugar for <code>React.createElement()</code>. It is an extension of JavaScript that allows developers to write HTML right within JavaScript. So when you're writing JSX, technically you're writing JavaScript and HTML together. </p>
<p>Also, that means JavaScript's reserved keywords must be kept intact. And that is why the “for” attribute in HTML is “HTMLFor” in JSX, since "for" is one of the commonest JavaScript reserved keywords.</p>
<p>In terms of support by browsers, HTML is supported by all of them. JSX, on the other hand, was never really intended to be, so you need a compiler like Babel or Webpack to transpile JSX into the JavaScript that browsers understand. </p>
<h2 id="heading-the-main-differences-between-html-and-jsx">The Main Differences Between HTML and JSX</h2>
<h3 id="heading-you-need-to-return-a-single-parent-element-in-jsx">You need to return a single parent element in JSX</h3>
<p>One of the major differences between HTML and JSX is that in JSX, you must return a single parent element, or it won't compile. </p>
<p>A lot of devs use <code>&lt;div&gt;...&lt;/div&gt;</code>, but a better one that many people use is “fragment”, <code>&lt;&gt;...&lt;/&gt;</code> which makes the code more readable. </p>
<p>In HTML, you are free to do whatever you want as you don’t have to return a single parent element.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/05/jsx1.png" alt="jsx1" width="600" height="400" loading="lazy">
Here you can see that JSX is not compiling because there's no parent element.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/05/jsx2.png" alt="jsx2" width="600" height="400" loading="lazy">
And here JSX is compiling because there is a parent element (fragment).</p>
<h3 id="heading-you-can-implement-js-directly-in-jsx">You can implement JS directly in JSX</h3>
<p>In JSX, it is possible to write JavaScript directly. You can do this by putting the JavaScript in curly braces <code>{...}</code>. Whereas in HTML, you need a script tag or an external JavaScript file to implement JavaScript:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> Article = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Hi campers<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>How's coding going<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>What is up?<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        {new Date().toDateString()}
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        {2 + 5} is seven in word
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Article;
</code></pre>
<h3 id="heading-all-tags-self-close-in-jsx">All Tags Self-close in JSX</h3>
<p>Tags can self-close in JSX. That is, it is possible to have <code>&lt;div&gt;&lt;/div&gt;</code> as <code>&lt;div /&gt;</code> and <code>&lt;span&gt;&lt;/span&gt;</code> as <code>&lt;span /&gt;</code>. You don't want to do that, but its possible. </p>
<p>Self-closing tags in HTML can self-close without the slash before the right angle bracket, that is <code>&lt;br /&gt;</code> could work as <code>&lt;br&gt;</code>. But in JSX, you need to include the slash. This should bring something to mind – JSX heavily relies on HTML 4 syntax.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/05/jsx3.png" alt="jsx3" width="600" height="400" loading="lazy">
Here you can see that JSX is not compiling because there's no forward slash before the right angle bracket of the line break tag.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/05/jsx4.png" alt="jsx4" width="600" height="400" loading="lazy">
And here you can see that JSX is compiling because there is a forward slash in the line break tag.</p>
<p>###ClassName and HTMLFor, not class and for in JSX
To define class names and for attributes in JSX, you don't do it as <code>class</code> or <code>for</code>, since both are reserved keywords in JavaScript. </p>
<p>You actually create class components with the <code>class</code> keyword. So, to define class names in JSX, you do it as "<code>className</code>" and for attributes for labels you write "<code>HTMLFor</code>":</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> Article = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Hi campers<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>How's coding going<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>What is up?<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        {new Date().toDateString()}
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        {2 + 5} is seven in word
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">form</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">htmlFor</span>=<span class="hljs-string">"name"</span>&gt;</span>Name<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Article;
</code></pre>
<h3 id="heading-write-all-html-attributes-in-camelcase-in-jsx">Write all HTML Attributes in camelCase in JSX</h3>
<p>You need to write all HTML attributes and event references in camelCase while writing JSX. So, <code>onclick</code> becomes <code>onClick</code>, <code>onmouseover</code> becomes <code>onMouseOver</code>, and so on:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> Article = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> sayHI = <span class="hljs-function">() =&gt;</span> {
    alert(<span class="hljs-string">"Hi Campers"</span>);
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Hi campers<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>How's coding going<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>What is up?<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        {new Date().toDateString()}
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        {2 + 5} is seven in word
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{sayHI}</span>&gt;</span>ALert Hi<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Article;
</code></pre>
<h3 id="heading-write-inline-styles-as-objects-in-jsx">Write Inline Styles as Objects in JSX</h3>
<p>And lastly, to define inline styles for JSX, you write it as an object, with the properties in camelCase, the values in quotes, and then you pass it inline to the JSX. </p>
<p>This means you have to open up a style attribute and use curly braces instead of quotes. This is opposed to HTML where you're free to define millions of styles right inside the opening tag without writing them as objects and putting them in quotes:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> Article = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> inlineStyle = {
    <span class="hljs-attr">color</span>: <span class="hljs-string">"#2ecc71"</span>,
    <span class="hljs-attr">fontSize</span>: <span class="hljs-string">"26px"</span>,
  };
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{inlineStyle}</span>&gt;</span>Hi campers<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>How's coding going<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>What is up?<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        {new Date().toDateString()}
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        {2 + 5} is seven in word
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{sayHI}</span>&gt;</span>ALert Hi<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Article;
</code></pre>
<p>You can choose to write the object directly in the style attribute – that is, by opening up two curly braces and putting the properties and values inside.</p>
<p>But a cleaner way is to define the object outside of the JSX, and then pass it into the opening tag.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> Article = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">color:</span> "#<span class="hljs-attr">2ecc71</span>", <span class="hljs-attr">fontSize:</span> "<span class="hljs-attr">26px</span>" }}&gt;</span>Hi campers<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>How's coding going<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>What is up?<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        {new Date().toDateString()}
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        {2 + 5} is seven in word
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Article;
</code></pre>
<p>Note that you should not use inline styling in plain HTML – you don't want to ruin your website's SEO.</p>
<h2 id="heading-thats-it">That's it!</h2>
<p>Thank you for reading. To connect with me, checkout my <a target="_blank" href="ksound22.github.io">portfolio</a>, or follow me on <a target="_blank" href="https://twitter.com/koladechris">Twitter</a>, where I spend most of my time tweeting and engaging in anything web development.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ The Complete React Tutorial for 2021 – Learn Major React Concepts by Building a Project ]]>
                </title>
                <description>
                    <![CDATA[ Welcome to the complete React tutorial for 2021. This guide should help you become effective with React as quickly as possible as you build a complete application along the way. Compared to many tutorials you might have gone through before, this one ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-tutorial-build-a-project/</link>
                <guid isPermaLink="false">66d037ecc1024fe75b758f54</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JSX ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react hooks ]]>
                    </category>
                
                    <category>
                        <![CDATA[ State Management  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Reed ]]>
                </dc:creator>
                <pubDate>Fri, 09 Apr 2021 18:18:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/04/the-complete-react-tutorial-2021.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Welcome to the complete React tutorial for 2021. This guide should help you become effective with React as quickly as possible as you build a complete application along the way.</p>
<p>Compared to many tutorials you might have gone through before, this one is meant to be thoroughly practical from start to finish. </p>
<p>You will learn how to create an entire React application all within around 100 lines of code, which makes use of many of the core concepts of React: hooks, state management, forms, JSX elements, components, props, styling, and conditionals. </p>
<p>And best of all, you will learn all of these concepts while coding yourself, hands-on. Let's get started!</p>
<h2 id="heading-how-to-bootstrap-our-react-project">How to Bootstrap our React Project</h2>
<p>We're going to create our React application by going to the website <a target="_blank" href="https://react.new">react.new</a>. </p>
<p>What this will do is create a new code sandbox for us. We can use code sandbox to create and develop complete React applications without having to install anything on our computer. </p>
<p>Once you visit react.new, you will see your code editor and, on the right hand side, we see a live version of our application to which we can make changes:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<blockquote>
<p>Quick tip: Make sure to hit command/ctrl S. Doing so will fork our sandbox and create a special URL that we can revisit in the future. </p>
</blockquote>
<p>Right now we're looking at our app component, which is the only component that's being displayed in our application. If we look at our file explorer on the left, we'll see app is being imported and rendered here within this index.js file. </p>
<pre><code class="lang-js"><span class="hljs-comment">// src/index.js</span>
<span class="hljs-keyword">import</span> { StrictMode } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">"react-dom"</span>;

<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">"./App"</span>;

<span class="hljs-keyword">const</span> rootElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>);
ReactDOM.render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">StrictMode</span>&gt;</span></span>,
  rootElement
);
</code></pre>
<p>What does all of this code do?</p>
<p>It simply "renders" or displays our app by injecting it into an index.html file, which is what we see on the right hand side of the page. </p>
<p>The code also finds and puts our app in the so-called root element (a div with the id of "root"). If you want see where that element is, you can find it within our public folder, specifically in the index.html file. </p>
<h2 id="heading-how-to-use-jsx">How to Use JSX</h2>
<p>Now that we have a working React app, let's start building it and changing what we see.</p>
<p>Let's begin within our div by removing this h2 element, and within our h1, just calling our app "Todo List": </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>What we are working with here is called <strong>JSX</strong>. It looks very similar to HTML, but is in fact JavaScript. We use it to build the structure of our application, just as we would use HTML. </p>
<blockquote>
<p>We can use any standard HTML elements within JSX: divs, any heading element, paragraph, spans, buttons, and so on. </p>
</blockquote>
<p>It's important to note that there are some minor differences between JSX and HTML. </p>
<p>The attributes that we use on JSX are slightly different than in normal HTML elements. They are written in the camelcase style, which is a standard way of writing variables or properties in JavaScript. </p>
<p>For example, to apply a class on a JSX element, we use an attribute called <code>className</code>. For normal HTML, that would just be called <code>class</code>. </p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Todo List<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>If we use <code>class</code> instead of <code>className</code> for JSX, we're going to get a warning saying class is an invalid DOM property:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-3.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-create-a-list-of-todo-elements">How to Create a List of Todo Elements</h2>
<p>Since we're making a todo application, let's create our todo list underneath our h1 header. </p>
<p>We could begin by making an unordered list with some list items as children elements. Each todo would be listed within an <code>li</code> element:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Todo List<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Todo Item<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>We can do something better as React developers, however. Instead, let's make a dedicated component that is responsible for displaying our todos. </p>
<h2 id="heading-how-to-create-new-react-components">How to Create New React Components</h2>
<p><strong>Components</strong> are the backbone of any React application. </p>
<p>We use components to separate different parts of our user interface. This makes them reusable wherever we need them across our app, it better organizes our code, and it makes it easier to understand our projects.</p>
<blockquote>
<p>Components fulfill an important concept in programming which is called "separation of concerns." This means it is preferable for each part of our component to have its own clearly defined role and responsibilities, separate from any other component.</p>
</blockquote>
<p>Just as we have an App component, we can create a component to be displayed within App. Since it is a list of todos, let's call it "TodoList":</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Todo List<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">TodoList</span> /&gt;</span> {/* component with single tag */}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-react-component-rules">React Component rules</h2>
<p>Every component must begin with a capital letter. And once a component is declared, it can be written and used very similarly to an HTML element. </p>
<p>A component can consist of just one tag or two tags. If it doesn't have anything between the two tags, which are called <strong>children</strong>, it should only have as one tag as the code above displays: <code>&lt;TodoList /&gt;</code>. </p>
<p>Additionally, if a component or element consists of just one tag, it must be self-closing. Meaning, it must end in a forward slash (like <code>&lt;TodoList /&gt;</code> and not <code>&lt;TodoList&gt;</code>).</p>
<p>We are attempting to display our TodoList component, but we haven't created it yet. To do that, we can create another function component like App, with the name TodoList. </p>
<p>At this point, we're going to get this error saying nothing was returned from render:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-4.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>We need to return something, specifically some JSX. Every component we make must return JSX elements and components (which must also, ultimately, be composed of JSX). </p>
<p>In our case, we want to return our list of todos. Let's take our unordered list with all of our list items that we want to show. We don't really have any data just yet, so let's create some.</p>
<p>In particular, let's create a set of todo data, which we can include in an array. Let's add this to the App component:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> todos = [
    { <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Wash dishes"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> },
    { <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Do laundry"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> },
    { <span class="hljs-attr">id</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Take shower"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> }
  ];

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Todo List<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">TodoList</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TodoList</span>(<span class="hljs-params"></span>) </span>{}
</code></pre>
<h2 id="heading-how-to-pass-data-to-components-with-props">How to Pass Data to Components with Props</h2>
<p>Now the question is – how do we pass all this data to and display it within our todo list?</p>
<p>With React components, we can do that with special properties that we add to the component called props. </p>
<p><strong>Props</strong> are custom attributes we can add to React components to pass data to our components. They are the React equivalent of arguments in JavaScript. </p>
<p>Since our data is called todos, let's name our prop the same: "todos". We use the equals operator to set a prop's value as well as a set of curly braces. This is because our todos array is a variable (a dynamic value):</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> todos = [
    { <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Wash dishes"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> },
    { <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Do laundry"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> },
    { <span class="hljs-attr">id</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Take shower"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> }
  ];

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Todo List<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">TodoList</span> <span class="hljs-attr">todos</span>=<span class="hljs-string">{todos}</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TodoList</span>(<span class="hljs-params"></span>) </span>{}
</code></pre>
<blockquote>
<p>If we wanted to make it a string, for example, we would wrap it in a set of quotes. But since this is a dynamic value that can change, we want to always include it within curly braces.</p>
</blockquote>
<p>Within the TodoList component, where are our props going to be received to ultimately display our todos data? They're going to be received exactly where any function would receive their arguments. </p>
<p>We receive our prop data on an object which we usually call "props", but we can give it whatever name we like. </p>
<p>We can see that we're passing this data down by using <code>console.log(props)</code>. If we look at our console tab, we have this property on our props object called "todos". </p>
<p>It has an array of three items just like we would expect:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> todos = [
    { <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Wash dishes"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> },
    { <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Do laundry"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> },
    { <span class="hljs-attr">id</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Take shower"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> }
  ];

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Todo List<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">TodoList</span> <span class="hljs-attr">todos</span>=<span class="hljs-string">{todos}</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TodoList</span>(<span class="hljs-params">props</span>) </span>{
  <span class="hljs-built_in">console</span>.log(props) <span class="hljs-comment">// {todos: Array(3)}</span>
}
</code></pre>
<h2 id="heading-how-to-map-over-array-items-with-the-map-function">How to Map Over Array Items with the Map Function</h2>
<p>In order to display each of these list items, we can take the array that is on <code>props.todos</code>. </p>
<p>In particular, we can use a special function that React gives us on the todos array called <strong>map</strong>. </p>
<p>Since we want to display this within TodoList, we once again need to use a set of curly braces to display it within our JSX. Using <code>props.todo.map</code>, we will map over this array just like we would a normal JavaScript array. </p>
<blockquote>
<p>The React map function is slightly different than the normal JavaScript map function because it is made to return and render JSX elements. </p>
</blockquote>
<p><code>.map()</code> accepts an inner function and in that function, we can get access to each todo. Using an arrow function, we can return each todo within its own JSX. </p>
<p>Finally, we can immediately return that JSX by wrapping it in a set of parentheses:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-5.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Within our inner function, we get access to each todo's data. To display that data, we can take each todo which we know is an object. We can use a set of curly braces to output the dynamic value of whatever is on <code>todo.text</code>. </p>
<p>When we do that, we can see our three todos:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-6.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-what-are-react-keys-and-why-they-matter">What are React Keys (and Why They Matter)?</h2>
<p>If we look at the console tab at the bottom we will see a warning, saying each child in the list should have a "unique key prop." </p>
<p>The reason for this is that React needs to keep track of the order of each of the items in our list. It does so with the help of a special React prop called a <strong>key</strong>. </p>
<blockquote>
<p>For a key, you generally want to use a unique identifier, a unique value that is only associated with one piece of data. In our case, to identify each todo's data we will use the unique number provided on <code>todo.id</code>.</p>
</blockquote>
<p>So why are keys important? It is important for React to figure out how it should appropriately update our user interface. If we were to update a todo's text or done value, the key is what tells React which todo item needs to be updated. </p>
<p>Once we add the key prop to the element or component that we're looping over, we no longer get that warning: </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-7.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-get-individual-props-with-destructuring">How to Get Individual Props with Destructuring</h2>
<p>Note that one additional shorthand is that instead of referencing the entire object within the TodoList, we can reference the individual properties on that object to make our code a little bit shorter by using object destructuring. </p>
<blockquote>
<p>Object destructuring is not a React concept, but a standard JavaScript feature that makes accessing object properties easier by immediately declaring them as individual variables.</p>
</blockquote>
<p>As of right now, we only have one prop being passed down to TodoList, so let's destructure that one prop, <code>todos</code>, individually.</p>
<p>To do so, we add a set of curly braces within our functions parameters, and just grab the property that we need from the props object. This means that we can change <code>props.todos</code> to just <code>todos</code>:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> todos = [
    { <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Wash dishes"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> },
    { <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Do laundry"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> },
    { <span class="hljs-attr">id</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Take shower"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> }
  ];

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Todo List<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">TodoList</span> <span class="hljs-attr">todos</span>=<span class="hljs-string">{todos}</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-comment">// using object destructuring on the props object</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TodoList</span>(<span class="hljs-params">{ todos }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {todos.map((todo) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{todo.id}</span>&gt;</span>{todo.text}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-how-to-add-new-todo-list-items">How to Add New Todo List Items</h2>
<p>Now what about adding some new todos to our list? </p>
<p>Underneath our TodoList component, let's add a new component that's responsible for adding new todos. A logical name for this would be "AddTodo". </p>
<p>We can create this underneath our to do list component. Let's have AddTodo return a form element that contains a basic text input and a submit button.</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> todos = [
    { <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Wash dishes"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> },
    { <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Do laundry"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> },
    { <span class="hljs-attr">id</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">text</span>: <span class="hljs-string">"Take shower"</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> }
  ];

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Todo List<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">TodoList</span> <span class="hljs-attr">todos</span>=<span class="hljs-string">{todos}</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">AddTodo</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TodoList</span>(<span class="hljs-params">{ todos }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {todos.map((todo) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{todo.id}</span>&gt;</span>{todo.text}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">AddTodo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">form</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Add todo"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span></span>
  );
}
</code></pre>
<blockquote>
<p>Note that any JSX element that consists of just one tag (such as our input) must end in a forward slash. If we do not include it, we're going to get a compiler error saying "unterminated JSX contents." </p>
</blockquote>
<p>Now the question is: how do we type into our input, submit our form, and have a new todo added to our todos array?</p>
<h2 id="heading-how-to-handle-form-submissions-in-react">How to Handle Form Submissions in React</h2>
<p>To take care of submitting our form, we need to start working with events in React. </p>
<p>In our case, we want to use the "submit" event when our form is submitted by our user and for React to handle that form submission by adding a new todo. </p>
<p>React adds a special prop to the form element called <code>onSubmit</code>. onSubmit accepts a function within a set of curly braces. Let's create a new function, which we will call <code>handleAddTodo</code>. </p>
<blockquote>
<p>Note that most functions that handle events in React are prefixed with the word "handle". It's ultimately up to you how you want to name your functions, but this is a helpful convention.</p>
</blockquote>
<p>It's important to note that this function should be created within the component itself (AddTodo), not outside of it. When <code>handleAddTodo</code> is passed to the <code>onSubmit</code> prop, it will be called when our form is submitted:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;

<span class="hljs-comment">// ...</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">AddTodo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleAddTodo</span>(<span class="hljs-params"></span>) </span>{}

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">onSubmit</span>=<span class="hljs-string">{handleAddTodo}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Add todo"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-how-to-prevent-default-form-behavior">How to Prevent Default Form Behavior</h2>
<p>When we click the submit button or hit the return key, data from the submit event is passed automatically to our function that's connected to onSubmit. We receive that event data in the parameters of <code>handleAddTodo</code>.</p>
<p>The first thing that we want to do with this event is call a method on it called <code>.preventDefault()</code>. This method prevents the default action whenever we submit a form:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;

<span class="hljs-comment">// ...</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">AddTodo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleAddTodo</span>(<span class="hljs-params">event</span>) </span>{
    event.preventDefault();
  }

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">onSubmit</span>=<span class="hljs-string">{handleAddTodo}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Add todo"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span></span>
  );
}
</code></pre>
<p>Whenever we submit a form, by default, the page is refreshed. We don't want that behavior with React – we want JavaScript to control whatever happens next. </p>
<p>After preventing a refresh, we want to get access to what was typed into the input to create a new todo with it. How do we do that? </p>
<h2 id="heading-how-to-access-form-data-on-submit">How to Access Form Data on Submit</h2>
<p>The way that we get access to all of the elements within our form is with the help of the property <code>event.target.elements</code>. </p>
<p>First of all, this will give us the event target, which is the form itself. <code>elements</code> is a property that will give us all of the elements within that form, including our input and our submit button. </p>
<p>If we were to console.log <code>event.target.elements</code> right now, submit our form, and look at our console, we see just an object with a couple of properties, one called "0", and one called "1". </p>
<p>This isn't very helpful to us, although we do see that it is our input and our button:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-8.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Instead, we want to get what was typed into our input. </p>
<p>To do so, we can add either an "id" or a "name" attribute to our input. Let's add the name attribute with a value of "addTodo". When we hit submit again, this will give us a new property on the elements object also called <code>addTodo</code>. From that reference, we can very easily get what was typed into it. </p>
<p>This allows us to use <code>event.target.elements.addTodo.value</code> to get what was typed in whatever text was typed in. When we do so, when we type text into our input, and hit submit, we see it logged to the console:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-9.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now that we have our text, we'll put it in a variable called "text". Using this, we want to create a new todo. </p>
<p>We know that each todo is an object and it has to consist of the properties id, text, and done. Let's create a variable <code>todo</code> and that will be equal to a new object where the id will be 4, the text will be equal to the text that we're getting from the elements object, and we can set done to false.</p>
<p>By default, new todos that are added are not going to be done:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;

<span class="hljs-comment">//...</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">AddTodo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleAddTodo</span>(<span class="hljs-params">event</span>) </span>{
    event.preventDefault();
    <span class="hljs-keyword">const</span> text = event.target.elements.addTodo.value;
    <span class="hljs-keyword">const</span> todo = {
      <span class="hljs-attr">id</span>: <span class="hljs-number">4</span>,
      text,
      <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span>
    };
  }

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">onSubmit</span>=<span class="hljs-string">{handleAddTodo}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"addTodo"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Add todo"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span></span>
  );
}
</code></pre>
<p>And finally, the big question is, how do we add this todo to our array, <code>todos</code>? </p>
<h2 id="heading-introduction-to-state-in-react">Introduction to State in React</h2>
<p>This is where the concept of state comes in. </p>
<p>Right now we're dealing with static data – there is no real way to update this todos array. To be clear, there <em>is</em> a way to do it using JavaScript, but what we are not currently able to do is tell React, even if we were to update it, that it needs to <strong>re-render</strong> this list. </p>
<p>In other words, to perform an update to our data and then show us the updated data in our view. So while we could update the data, we also need React to show our users the updated data. </p>
<p><strong>State</strong> is required to fix our problem. </p>
<blockquote>
<p>State is a means of managing our application data and also allows React to update our UI (user interface) in response to data changes. </p>
</blockquote>
<h2 id="heading-how-to-manage-state-in-react-with-the-usestate-hook">How to Manage State in React with the useState Hook</h2>
<p>We can manage state in React using the <code>useState</code> hook. To use the useState hook, the first thing that we need to do is import React up at the top, because useState comes from the core React library. </p>
<p>After that, we can simply call the useState hook up at the top of our app component. Once we call useState just like a normal function, we will pass in our entire array of todos as our initial data. Our application will break for a moment since we're no we're no longer showing our todos just yet. </p>
<p>useState returns an array with two elements:</p>
<ol>
<li>The initial value we called useState with (our array of todos) and this becomes our state variable</li>
<li>A special function that allows us to update what is stored in the state variable </li>
</ol>
<p>We can destructure the values that are returned from useState by adding a set of array brackets to immediately get the values that are returned from it. First the state and second, the function to update the state:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-10.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>We'll call our state variable <code>todos</code> and the setter to manage our state <code>setTodos</code>. </p>
<p>All we have to do to update our state is to pass it, whatever we want the new state to be. This <code>setTodos</code> function is going to be passed down to our AddTodo component, so let's add that as a prop of the same name. We'll also destructure <code>setTodos</code> from our props object within AddTodo. </p>
<p>And finally, we can call <code>setTodos</code> at the bottom of <code>handleAddTodo</code>. What's great about this function is instead of having to pass down the todos array as well, this function can give us the previous state with the help of a function that we can receive inside of it:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-11.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>This may seem strange at first, but within <code>setTodos</code> we get access to the previous todo data. If we write an arrow function or any function for that matter, we can simply provide what we want the new state to be. </p>
<blockquote>
<p>The benefit of being able to access the previous state variable's value directly within the setter function is that it prevents us from having to pass down the entire todos state variable as an additional prop to every component in which we want to update its value.</p>
</blockquote>
<p>If we wanted to empty our todos state, we could just return an empty array right here. If we were to submit our form, we would see that all of our todos were removed. </p>
<p>Once we submit our form, state is updated, and our app is re-rendered as a result. </p>
<h2 id="heading-re-renders-in-react">Re-renders in React</h2>
<p>Note that any re-render within a parent component will cause any child components to re-render. That means whenever our todo data is updated, the TodoList component (a child of the App component) is updated with that new data. </p>
<p>If we go back to <code>handleAddTodo</code>, we can take our previous todos and use the <code>.concat()</code> method to add this new todo to our array in state. All we have to do is return this expression. </p>
<p>Let's add a new todo, such as "Balance Checkbook." Once we hit submit, we see that immediately added to our list:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-12.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now there's one problem here: we aren't clearing out our input after our form is submitted. </p>
<p>This means if we wanted to add another todo we would have to manually clear it out. How do we take this input's value and clear it out?</p>
<h2 id="heading-react-refs-and-useref">React refs and useRef</h2>
<p>To perform common actions such as clearing out an input's value or focusing our input we can use what's called a <strong>ref</strong>. </p>
<blockquote>
<p>A ref is a feature that React provides to reference to a given DOM element. </p>
</blockquote>
<p>In this case, we want a reference to this input element with the name of "addTodo." </p>
<p>Just like our state, we can work with refs by calling the appropriate React hook. To create a ref, we just need to call <code>React.useRef()</code> at the top of AddTodo. We don't have to pass it an initial value, but we can give it a default value if we needed to. </p>
<p>We will call this created ref <code>inputRef</code>. Using inputRef, we can create a reference to our input element which we can access wherever we like by using the built-in ref prop by setting <code>ref={inputRef}</code>:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;

<span class="hljs-comment">//...</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">AddTodo</span>(<span class="hljs-params">{ setTodos }</span>) </span>{
  <span class="hljs-keyword">const</span> inputRef = React.useRef();

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleAddTodo</span>(<span class="hljs-params">event</span>) </span>{
    event.preventDefault();
    <span class="hljs-keyword">const</span> text = event.target.elements.addTodo.value;
    <span class="hljs-keyword">const</span> todo = {
      <span class="hljs-attr">id</span>: <span class="hljs-number">4</span>,
      text,
      <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span>
    };
    setTodos(<span class="hljs-function">(<span class="hljs-params">prevTodos</span>) =&gt;</span> {
      <span class="hljs-keyword">return</span> prevTodos.concat(todo);
    });
  }

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">onSubmit</span>=<span class="hljs-string">{handleAddTodo}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"addTodo"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Add todo"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">{inputRef}</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span></span>
  );
}
</code></pre>
<p>What does this do? It allows us within <code>handleAddTodo</code> to use the property <code>inputRef.current</code>, which contains the input element itself. If we were to log <code>input.ref.current</code>, we would see our input element. </p>
<p>We have a direct reference to our input, which means we access any property that we like off of it. In our case, we want to take the value of the input on the value property. To clear the value from our input, we can just mutate inputRef directly by setting value to an empty string:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-13.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Whenever we hit submit, our input is cleared out without having to clear it out ourselves manually.</p>
<h2 id="heading-essential-rules-of-react-hooks">Essential Rules of React Hooks</h2>
<p>Since useRef is another React hook, we're starting to see some common features among React hooks. They are often prefixed with the word "use". In fact, most all React hooks have this prefix to denote that they are hooks and should be used as such. </p>
<p>Additionally, React hooks are called up at the very top of function components. Hooks cannot be used within class components. And finally, hooks cannot be conditional (that is, used within an if statement).</p>
<p>But as you can see, there's nothing too special about React hooks. They operate very much like regular JavaScript functions. </p>
<h2 id="heading-how-to-mark-todos-as-done-with-onclick">How to Mark Todos as Done with onClick</h2>
<p>After creating todos, we want to toggle them done – to strike through them if we've finished a given todo. How do we add this feature? </p>
<p>If we go back to our list item, within TodoList, we can see what that will look like by applying some inline styles. We saw how to add styles through classes. For styles that we want to apply inline to any given element, we cannot use the same syntax as we would with normal HTML. </p>
<p>If we tried to using the HTML syntax, we're going to get an error telling us "the style prop expects style properties within an object, not within a string":</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-14.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>To fix this, we will provide an object. We need to provide this object within another set of curly braces. Then, we will provide any property like we would in a normal JavaScript object to apply this strike through style. </p>
<p>For each of our list items, we can set the property <code>textDecoration</code> to "line-through":</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-15.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>We do not want every item to be struck through, we only want this to be applied if a given todo is done. How do we do that? </p>
<p>We can use a normal JavaScript conditional, in particular a ternary, to say that if a given todo's property done is true, then we want to apply the strike through value for text decoration, otherwise not. </p>
<p>If we change one of our todos array to have a done value of <code>true</code>, we see that that style rule is applied:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>

<span class="hljs-comment">//...</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TodoList</span>(<span class="hljs-params">{ todos }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {todos.map((todo) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>
          <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span>
            <span class="hljs-attr">textDecoration:</span> <span class="hljs-attr">todo.done</span> ? "<span class="hljs-attr">line-through</span>" <span class="hljs-attr">:</span> ""
          }}
          <span class="hljs-attr">key</span>=<span class="hljs-string">{todo.id}</span>
        &gt;</span>
          {todo.text}
        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}

<span class="hljs-comment">//...</span>
</code></pre>
<p>How do we actually toggle that todo? </p>
<p>We might want our user to click or double click on our todo in order to strike through it. That means we want to see how to register and handle a new type of event – a click event. </p>
<p>To handle a click event with React we provide the <code>onClick</code> prop to a given element for which we want to register that event. In this case, it's the <code>li</code> element. </p>
<p>Once again, we need to connect it to a function to handle our click event. We're going to call this <code>handleToggleTodo</code> and create it within our TodoList component. In this case, our function that we use to handle the event doesn't have to receive any event data. This function will handle updating our todo's state. </p>
<p>We want <code>handleToggleTodo</code> to go through the <code>todos</code> array and see if the one that the user has clicked on exists in our array. If so, its done value can be toggled to the opposite boolean value. </p>
<p>To receive the appropriate todo data for the appropriate list item that is clicked on, we can call <code>handleToggleTodo</code> as an inline arrow function and pass the todo data as an argument: </p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>

<span class="hljs-comment">//...</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TodoList</span>(<span class="hljs-params">{ todos }</span>) </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleToggleTodo</span>(<span class="hljs-params">todo</span>) </span>{}

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {todos.map((todo) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>
          <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> handleToggleTodo(todo)}
          style={{
            textDecoration: todo.done ? "line-through" : ""
          }}
          key={todo.id}
        &gt;
          {todo.text}
        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}

<span class="hljs-comment">//...</span>
</code></pre>
<p>To update our todos state, we'll pass down <code>setTodos</code> to our TodoList component. We'll pass down <code>setTodos</code> as a prop to TodoList, and destructure it from the props object. </p>
<p>Once again, we can call <code>setTodos</code> and get access to the previous todos by including an inner function. First, what we can do is take our entire todos array and map over it with the <code>.map()</code> array function. </p>
<p>In the inner function passed to map, we will check that the todos id we're mapping over is equal to the todo that we've clicked on. If so, we return a new object with all of the previous todo's properties, but with <code>done</code> toggled to its opposite boolean value:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>

<span class="hljs-comment">//...</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TodoList</span>(<span class="hljs-params">{ todos, setTodos }</span>) </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleToggleTodo</span>(<span class="hljs-params">todo</span>) </span>{
    <span class="hljs-comment">// confused by this code? Here's what it says:</span>

    <span class="hljs-comment">// if a todo's id is equal to the one we clicked on,</span>
    <span class="hljs-comment">// just update that todo's done value to its opposite,</span>
    <span class="hljs-comment">// otherwise, do nothing (return it)</span>

    <span class="hljs-keyword">const</span> updatedTodos = todos.map(<span class="hljs-function">(<span class="hljs-params">t</span>) =&gt;</span>
      t.id === todo.id
        ? {
            ...t,
            <span class="hljs-attr">done</span>: !t.done
          }
        : t
    );
  }

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {todos.map((todo) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>
          <span class="hljs-attr">onDoubleClick</span>=<span class="hljs-string">{()</span> =&gt;</span> handleToggleTodo(todo)}
          style={{
            textDecoration: todo.done ? "line-through" : ""
          }}
          key={todo.id}
        &gt;
          {todo.text}
          <span class="hljs-tag">&lt;<span class="hljs-name">DeleteTodo</span> <span class="hljs-attr">todo</span>=<span class="hljs-string">{todo}</span> <span class="hljs-attr">setTodos</span>=<span class="hljs-string">{setTodos}</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}

<span class="hljs-comment">//...</span>
</code></pre>
<p>Otherwise, if that todo that we're iterating over is not the one that we clicked on, we just want to return it (without changing it). This updated array is what we'll pass to <code>setTodos</code> to update our state. </p>
<p>If we click on a todo, we toggle it done. If we click on it again, it's toggled back to undone:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-16.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>For this to work appropriately, to see that a past todo's id is equal to the todo that we're clicking on, we need to make sure that each todo's id is unique. </p>
<p>Instead of setting each new todo to have an id of 4, we can just use <code>Math.random()</code> to make a semi-random value and ensure there are no list items with the same id. </p>
<p>Finally, as an alternative to <code>onClick</code>, we can use another event prop, <code>onDoubleClick</code>,  in the event that users accidentally click a given todo. Now if a user double clicks a list item, only then do we toggle it done. </p>
<h2 id="heading-how-to-handle-deleting-todos">How to Handle Deleting Todos</h2>
<p>The final bit of functionality that we're looking for is to be able to delete a given todo. </p>
<p>We can add that functionality within TodoList by adding another nested component. Underneath our todo text, we'll add a new component: DeleteTodo. Let's declare this new component above where we declared AddTodo. </p>
<p>What will this component consist of? In it, we will return a span, which will function like a button for us. A user can click on this and delete a given todo. </p>
<blockquote>
<p>If you want a non-button element to operate like a button, we need to make its "role" property set to "button". </p>
</blockquote>
<p>To our span, let's add some style rules – we can give it a color of red, make it bold, and separate it from the todo text by setting <code>marginLeft: 10</code>. What's neat about the style object is that we don't have to say 10 pixels as a string – we can use the value 10 or include any integer that we like.</p>
<p>Here is the code for our DeleteTodo component so far:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/tutorial-17.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>To delete a todo, we want to be able to click on it and show a confirmation dialog. If the user confirms they want to delete it, only then is the todo removed. </p>
<p>Since we're mapping over each todo item, including DeleteTodo, we can pass down a prop called just <code>todo</code> with each todo's data on it. </p>
<p>In DeleteTodo, on our span element, we want to add an <code>onClick</code> to handle deleting our todo. To handle this, we will call a new function: <code>handleDeleteTodo</code>. </p>
<p>Using this function, we first want to show a confirmation dialog. We can do so by saying <code>window.confirm()</code> with the message, "Do you want to delete this"? <code>window.confirm</code> is going to return a value of true or false based on whether the user has confirmed the dialog or not. We'll put the result of this action in a variable called <code>confirmed</code>:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-comment">// ...</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TodoList</span>(<span class="hljs-params">{ todos, setTodos }</span>) </span>{
  <span class="hljs-comment">// ...</span>

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {todos.map((todo) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>
          <span class="hljs-attr">onDoubleClick</span>=<span class="hljs-string">{()</span> =&gt;</span> handleToggleTodo(todo)}
          style={{
            textDecoration: todo.done ? "line-through" : ""
          }}
          key={todo.id}
        &gt;
          {todo.text}
          {/* pass todo data down as a prop to DeleteTodo */}
          <span class="hljs-tag">&lt;<span class="hljs-name">DeleteTodo</span> <span class="hljs-attr">todo</span>=<span class="hljs-string">{todo}</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">DeleteTodo</span>(<span class="hljs-params">{ todo, setTodos }</span>) </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleDeleteTodo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> confirmed = <span class="hljs-built_in">window</span>.confirm(<span class="hljs-string">"Do you want to delete this?"</span>);
    <span class="hljs-keyword">if</span> (confirmed) {
      <span class="hljs-comment">// take care of deleting the todo</span>
    }
  }

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">span</span>
      <span class="hljs-attr">onClick</span>=<span class="hljs-string">{handleDeleteTodo}</span>
      <span class="hljs-attr">role</span>=<span class="hljs-string">"button"</span>
      <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span>
        <span class="hljs-attr">color:</span> "<span class="hljs-attr">red</span>",
        <span class="hljs-attr">fontWeight:</span> "<span class="hljs-attr">bold</span>",
        <span class="hljs-attr">marginLeft:</span> <span class="hljs-attr">10</span>,
        <span class="hljs-attr">cursor:</span> "<span class="hljs-attr">pointer</span>"
      }}
    &gt;</span>
      x
    <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span></span>
  );
}

<span class="hljs-comment">//...</span>
</code></pre>
<p>If <code>confirmed</code> is true, only then do we want to delete the todo.</p>
<p>To do that, we need to use <code>setTodos</code> once again. We'll pass it down one more level from TodoList to the DeleteTodo component and destructure it from the props object. </p>
<p>Then, within <code>handleDeleteTodo</code>, we can call it and use the inner function to get the previous todos. To remove the todo that a user has clicked on, we can filter through this array to make sure that we are removing the one that the user selected. </p>
<p>To do so, we make sure all the todos in our array do not have an id equal to the one we are attempting to delete:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>

<span class="hljs-comment">// ...</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">DeleteTodo</span>(<span class="hljs-params">{ todo, setTodos }</span>) </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleDeleteTodo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> confirmed = <span class="hljs-built_in">window</span>.confirm(<span class="hljs-string">"Do you want to delete this?"</span>);
    <span class="hljs-keyword">if</span> (confirmed) {
      setTodos(<span class="hljs-function">(<span class="hljs-params">prevTodos</span>) =&gt;</span> {
        <span class="hljs-keyword">return</span> prevTodos.filter(<span class="hljs-function">(<span class="hljs-params">t</span>) =&gt;</span> t.id !== todo.id);
      });
    }
  }

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">span</span>
      <span class="hljs-attr">onClick</span>=<span class="hljs-string">{handleDeleteTodo}</span>
      <span class="hljs-attr">role</span>=<span class="hljs-string">"button"</span>
      <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span>
        <span class="hljs-attr">color:</span> "<span class="hljs-attr">red</span>",
        <span class="hljs-attr">fontWeight:</span> "<span class="hljs-attr">bold</span>",
        <span class="hljs-attr">marginLeft:</span> <span class="hljs-attr">10</span>,
        <span class="hljs-attr">cursor:</span> "<span class="hljs-attr">pointer</span>"
      }}
    &gt;</span>
      x
    <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span></span>
  );
}

<span class="hljs-comment">// ...</span>
</code></pre>
<p>Now if we attempt to delete one of our todos, we see our confirmation dialog, we hit "ok", and immediately it's removed from our list. </p>
<p>If we delete all of our todos, we no longer see anything. If we want to tell our user there are no todos in the list when the array is empty, let's head up to our TodoList component. </p>
<p>If we have an empty todos array, we can add a conditional above our return and check if our array's length is equal to 0. If so, we will display a paragraph element with the text "No todos left":</p>
<pre><code class="lang-js"><span class="hljs-comment">// ...</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TodoList</span>(<span class="hljs-params">{ todos, setTodos }</span>) </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleToggleTodo</span>(<span class="hljs-params">todo</span>) </span>{
    <span class="hljs-keyword">const</span> updatedTodos = todos.map(<span class="hljs-function">(<span class="hljs-params">t</span>) =&gt;</span>
      t.id === todo.id
        ? {
            ...t,
            <span class="hljs-attr">done</span>: !t.done
          }
        : t
    );
    setTodos(updatedTodos);
  }

  <span class="hljs-keyword">if</span> (!todos.length) {
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>No todos left!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>;
  }

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {todos.map((todo) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>
          <span class="hljs-attr">onDoubleClick</span>=<span class="hljs-string">{()</span> =&gt;</span> handleToggleTodo(todo)}
          style={{
            textDecoration: todo.done ? "line-through" : ""
          }}
          key={todo.id}
        &gt;
          {todo.text}
          <span class="hljs-tag">&lt;<span class="hljs-name">DeleteTodo</span> <span class="hljs-attr">todo</span>=<span class="hljs-string">{todo}</span> <span class="hljs-attr">setTodos</span>=<span class="hljs-string">{setTodos}</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}

<span class="hljs-comment">// ...</span>
</code></pre>
<h2 id="heading-congratulations">Congratulations!</h2>
<p>You now have a working todo app that has full CRUD functionality that can create, read, update, and delete todos.</p>
<p>You've been able to see how many of the major React concepts work firsthand and you're now in a great position to start building your own React applications.</p>
<p>If you would like to take a look at our final app code, you can see it <a target="_blank" href="https://codesandbox.io/s/late-firefly-ker6p">here</a>.</p>
<h2 id="heading-become-a-professional-react-developer">Become a Professional React Developer</h2>
<p>React is hard. You shouldn't have to figure it out yourself.</p>
<p>I've put everything I know about React into a single course, to help you reach your goals in record time:</p>
<p><a target="_blank" href="https://www.thereactbootcamp.com"><strong>Introducing: The React Bootcamp</strong></a></p>
<p><strong>It’s the one course I wish I had when I started learning React.</strong></p>
<p>Click below to try the React Bootcamp for yourself:</p>
<p><a target="_blank" href="https://www.thereactbootcamp.com"><img src="https://reedbarger.nyc3.digitaloceanspaces.com/reactbootcamp/react-bootcamp-cta-alt.png" alt="Click to join the React Bootcamp" width="600" height="400" loading="lazy"></a>
<em>Click to get started</em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Write Cleaner React Code ]]>
                </title>
                <description>
                    <![CDATA[ As React developers, we all want to write cleaner code that is simpler and easier to read.  In this guide, I've put together seven of the top ways that you can start writing cleaner React code today to make building React projects and reviewing your ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-write-cleaner-react-code/</link>
                <guid isPermaLink="false">66d03791871ae63f179f6bb1</guid>
                
                    <category>
                        <![CDATA[ clean code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JSX ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Reed ]]>
                </dc:creator>
                <pubDate>Mon, 05 Apr 2021 20:29:10 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/04/7-ways-to-write-clean-react-code.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>As React developers, we all want to write cleaner code that is simpler and easier to read. </p>
<p>In this guide, I've put together seven of the top ways that you can start writing cleaner React code today to make building React projects and reviewing your code much easier.</p>
<p>In general, learning how to write cleaner React code will make you a more valuable and overall happier React developer, so let's jump right in!</p>
<h2 id="heading-1-make-use-of-jsx-shorthands">1. Make use of JSX shorthands</h2>
<p>How do you pass a value of true to a given prop? </p>
<p>In the example below, we're using the prop <code>showTitle</code> to display the title of our app within a Navbar component. </p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> <span class="hljs-attr">showTitle</span>=<span class="hljs-string">{true}</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params">{ showTitle }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      {showTitle &amp;&amp; <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>My Special App<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  )
}
</code></pre>
<p>Do we need to explicitly set <code>showTitle</code> to the Boolean <code>true</code>? We don't! A quick shorthand to remember is that any prop provided on a component has a default value of true. </p>
<p>So if we add the prop <code>showTitle</code> on Navbar, our title element will be shown:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> <span class="hljs-attr">showTitle</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params">{ showTitle }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      {showTitle &amp;&amp; <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>My Special App<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>} // title shown!
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  )
}
</code></pre>
<p>Another useful shorthand to remember involves passing string props. When you're passing a prop value that's a string, you don't need to wrap it in curly braces. </p>
<p>If we are setting the title of our Navbar, with the <code>title</code> prop, we can just include its value in double quotes:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"My Special App"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params">{ title }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{title}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  )
}
</code></pre>
<h2 id="heading-2-move-unrelated-code-into-a-separate-component">2. Move unrelated code into a separate component</h2>
<p>Arguably the easiest and most important way to write cleaner React code is to get good at abstracting our code into separate React components. </p>
<p>Let's look at the example below. What is our code doing? </p>
<p>Our app is displaying a Navbar component. We are iterating over an array of posts with <code>.map()</code> and displaying their title on the page. </p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> posts = [
    {
      <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>,
      <span class="hljs-attr">title</span>: <span class="hljs-string">"How to Build YouTube with React"</span>
    },
    {
      <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>,
      <span class="hljs-attr">title</span>: <span class="hljs-string">"How to Write Your First React Hook"</span>
    }
  ];

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"My Special App"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
        {posts.map(post =&gt; (
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{post.id}</span>&gt;</span>
            {post.title}
          <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        ))}
      <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params">{ title }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{title}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>How can we make this cleaner? </p>
<p>Why don't we abstract the code that we're looping over – our posts – and display them in a separate component, which we'll call FeaturedPosts. </p>
<p>Let's do that and take a look at the result:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
 <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"My Special App"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">FeaturedPosts</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params">{ title }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{title}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">FeaturedPosts</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> posts = [
    {
      <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>,
      <span class="hljs-attr">title</span>: <span class="hljs-string">"How to Build YouTube with React"</span>
    },
    {
      <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>,
      <span class="hljs-attr">title</span>: <span class="hljs-string">"How to Write Your First React Hook"</span>
    }
  ];

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {posts.map((post) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{post.id}</span>&gt;</span>{post.title}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}
</code></pre>
<p>As you can see, we can now just look at our App component. By reading the names of the components within it, Navbar and FeaturedPosts, we see exactly what our app is displaying. </p>
<h2 id="heading-3-create-separate-files-for-each-component">3. Create separate files for each component</h2>
<p>Going off of our previous example, we are including all of our components within a single file, the app.js file. </p>
<p>Similar to how we abstract code into separate components to make our app more readable, to make our application files more readable, we can put each component that we have into a separate file.</p>
<p>This, again, helps us separate concerns in our application. This means that each file is responsible for just one component and there's no confusion where a component comes from if we want to reuse it across our app:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>
<span class="hljs-keyword">import</span> Navbar <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Navbar.js'</span>;
<span class="hljs-keyword">import</span> FeaturedPosts <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/FeaturedPosts.js'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"My Special App"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">FeaturedPosts</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
  );
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-comment">// src/components/Navbar.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params">{ title }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{title}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-comment">// src/components/FeaturedPosts.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">FeaturedPosts</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> posts = [
    {
      <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>,
      <span class="hljs-attr">title</span>: <span class="hljs-string">"How to Build YouTube with React"</span>
    },
    {
      <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>,
      <span class="hljs-attr">title</span>: <span class="hljs-string">"How to Write Your First React Hook"</span>
    }
  ];

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {posts.map((post) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{post.id}</span>&gt;</span>{post.title}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}
</code></pre>
<p>Additionally, by including each individual component within its own file, we avoid one file becoming too bloated. We could easily see our app.js file becoming very large if we wanted to add all of our components into that file. </p>
<h2 id="heading-4-move-shared-functionality-into-react-hooks">4. Move shared functionality into React hooks</h2>
<p>Taking a look at our FeaturedPosts component, let's say instead of displaying static posts data, we want to fetch our post data from an API. </p>
<p>We might do so with the fetch API. You can see the result below for that: </p>
<pre><code class="lang-js"><span class="hljs-comment">// src/components/FeaturedPosts.js</span>

<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">FeaturedPosts</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [posts, setPosts] = React.useState([]);      

  React.useEffect(<span class="hljs-function">() =&gt;</span> {
    fetch(<span class="hljs-string">'https://jsonplaceholder.typicode.com/posts'</span>)
      .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> res.json())
      .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> setPosts(data));
  }, []);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {posts.map((post) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{post.id}</span>&gt;</span>{post.title}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}
</code></pre>
<p>However, what if we wanted to perform this request for data across multiple components? </p>
<p>Let's say in addition to a FeaturedPosts component we wanted to create a component called just Posts with the same data. We would have to copy the logic that we used to fetch our data and paste it within that component as well. </p>
<p>To avoid having to do that, why don't we just use a new React hook which we could call <code>useFetchPosts</code>:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/hooks/useFetchPosts.js</span>

<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">useFetchPosts</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [posts, setPosts] = React.useState([]);      

  React.useEffect(<span class="hljs-function">() =&gt;</span> {
    fetch(<span class="hljs-string">'https://jsonplaceholder.typicode.com/posts'</span>)
      .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> res.json())
      .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> setPosts(data));
  }, []);

  <span class="hljs-keyword">return</span> posts;
}
</code></pre>
<p>Once we've created this hook in a dedicated 'hooks' folder we can reuse it in whichever components we like, including our FeaturedPosts component:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/components/FeaturedPosts.js</span>

<span class="hljs-keyword">import</span> useFetchPosts <span class="hljs-keyword">from</span> <span class="hljs-string">'../hooks/useFetchPosts.js'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">FeaturedPosts</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> posts = useFetchPosts()

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {posts.map((post) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{post.id}</span>&gt;</span>{post.title}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-5-remove-as-much-javascript-from-your-jsx-as-possible">5. Remove as much JavaScript from your JSX as possible</h2>
<p>Another very helpful, but often neglected way to clean up our components is to remove as much JavaScript from our JSX as possible. </p>
<p>Let's take a look at the example below:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/components/FeaturedPosts.js</span>

<span class="hljs-keyword">import</span> useFetchPosts <span class="hljs-keyword">from</span> <span class="hljs-string">'../hooks/useFetchPosts.js'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">FeaturedPosts</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> posts = useFetchPosts()

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {posts.map((post) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{event</span> =&gt;</span> {
          console.log(event.target, 'clicked!');
        }} key={post.id}&gt;{post.title}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}
</code></pre>
<p>We're trying to handle a click event on one of our posts. You can see that our JSX becomes much harder to read. Given that our function is included as an inline function, it obscures the purpose of this component, as well as its related functions. </p>
<p>What can we do to fix this? We can extract the inline function, connected to the <code>onClick</code> into a separate handler, which we can give a an appropriate name like <code>handlePostClick</code>.</p>
<p>Once we do, our JSX becomes readable once again:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/components/FeaturedPosts.js</span>

<span class="hljs-keyword">import</span> useFetchPosts <span class="hljs-keyword">from</span> <span class="hljs-string">'../hooks/useFetchPosts.js'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">FeaturedPosts</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> posts = useFetchPosts()

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handlePostClick</span>(<span class="hljs-params">event</span>) </span>{
    <span class="hljs-built_in">console</span>.log(event.target, <span class="hljs-string">'clicked!'</span>);   
  }

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {posts.map((post) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{handlePostClick}</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{post.id}</span>&gt;</span>{post.title}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-6-format-inline-styles-for-less-bloated-code">6. Format inline styles for less bloated code</h2>
<p>A common pattern for React developers is to write inline styles in their JSX. But once again, this makes our code harder to read and harder to write additional JSX:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">textAlign:</span> '<span class="hljs-attr">center</span>' }}&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"My Special App"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params">{ title }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">marginTop:</span> '<span class="hljs-attr">20px</span>' }}&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">fontWeight:</span> '<span class="hljs-attr">bold</span>' }}&gt;</span>{title}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  )
}
</code></pre>
<p>We want to apply this concept of separation of concerns to our JSX styles by moving our inline styles into a CSS stylesheet, which we can import into whatever component we like. </p>
<p>An alternative way to rewrite your inline styles is by organizing them into objects. You can see what such a pattern would look like below:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> styles = {
    <span class="hljs-attr">main</span>: { <span class="hljs-attr">textAlign</span>: <span class="hljs-string">"center"</span> }
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.main}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"My Special App"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params">{ title }</span>) </span>{
  <span class="hljs-keyword">const</span> styles = {
    <span class="hljs-attr">div</span>: { <span class="hljs-attr">marginTop</span>: <span class="hljs-string">"20px"</span> },
    <span class="hljs-attr">h1</span>: { <span class="hljs-attr">fontWeight</span>: <span class="hljs-string">"bold"</span> }
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.div}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.h1}</span>&gt;</span>{title}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-7-reduce-prop-drilling-with-react-context">7. Reduce prop drilling with React context</h2>
<p>Another essential pattern to employ for your React projects (especially if you have common properties that you want to reuse across your components, and you find yourself writing lots of duplicate props) is to use React Context. </p>
<p>For example, if we wanted to share user data across multiple components, instead of multiple repeat props (a pattern called props drilling), we could use the context feature that's built into the React library. </p>
<p>In our case, if we wanted to reuse user data across our Navbar and FeaturedPosts components, all we would need to do is wrap our entire app in a provider component. </p>
<p>Next, we can pass the user data down on the value prop and consume that context in our individual components with the help of the <code>useContext</code> hook:</p>
<pre><code class="lang-js"><span class="hljs-comment">// src/App.js</span>

<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">const</span> UserContext = React.createContext();

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> user = { <span class="hljs-attr">name</span>: <span class="hljs-string">"Reed"</span> };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">UserContext.Provider</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{user}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"My Special App"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">FeaturedPosts</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">UserContext.Provider</span>&gt;</span></span>
  );
}

<span class="hljs-comment">// src/components/Navbar.js</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params">{ title }</span>) </span>{
  <span class="hljs-keyword">const</span> user = React.useContext(UserContext);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{title}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      {user &amp;&amp; <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/logout"</span>&gt;</span>Logout<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-comment">// src/components/FeaturedPosts.js</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">FeaturedPosts</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> posts = useFetchPosts();
  <span class="hljs-keyword">const</span> user = React.useContext(UserContext);

  <span class="hljs-keyword">if</span> (user) <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {posts.map((post) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{post.id}</span>&gt;</span>{post.title}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>I hope you find this guide useful when you're trying to improve your own React code to make it cleaner, easier to read, and ultimately more enjoyable to create your React projects.</p>
<h2 id="heading-become-a-professional-react-developer">Become a Professional React Developer</h2>
<p>React is hard. You shouldn't have to figure it out yourself.</p>
<p>I've put everything I know about React into a single course, to help you reach your goals in record time:</p>
<p><a target="_blank" href="https://www.thereactbootcamp.com"><strong>Introducing: The React Bootcamp</strong></a></p>
<p><strong>It’s the one course I wish I had when I started learning React.</strong></p>
<p>Click below to try the React Bootcamp for yourself:</p>
<p><a target="_blank" href="https://www.thereactbootcamp.com"><img src="https://reedbarger.nyc3.digitaloceanspaces.com/reactbootcamp/react-bootcamp-cta-alt.png" alt="Click to join the React Bootcamp" width="600" height="400" loading="lazy"></a>
<em>Click to get started</em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Why You Should Use React Components Instead of HTML ]]>
                </title>
                <description>
                    <![CDATA[ HTML is the language of the web, but creating entire websites with HTML alone can be repetitive and hard to manage. In this article, we're going to see how to use the JavaScript library React as a way to add convenience and reusability to our website... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/intro-to-react-components/</link>
                <guid isPermaLink="false">66d03794dcd3a41034854bc2</guid>
                
                    <category>
                        <![CDATA[ components ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JSX ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Reed ]]>
                </dc:creator>
                <pubDate>Wed, 17 Mar 2021 15:34:07 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/03/still-using-html-start-using-react-components.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>HTML is the language of the web, but creating entire websites with HTML alone can be repetitive and hard to manage.</p>
<p>In this article, we're going to see how to use the JavaScript library React as a way to add convenience and reusability to our websites.</p>
<p>React is a powerful tool for any developer who knows HTML and wants to build more organized and dynamic websites, faster.</p>
<p>Let's get started!</p>
<h2 id="heading-why-should-i-use-react-instead-of-html">Why should I use React instead of HTML?</h2>
<p>React arrived in 2013 as a better way to build web apps with JavaScript. It's often referred to as a library for building UIs, short for "user interfaces". </p>
<p>What makes React such a desirable library to learn is that <em>it doesn't replace HTML.</em></p>
<p>It takes advantage of HTML's popularity and strength as the most popular programming language, by letting you use a very similar syntax to HTML to build interfaces and add dynamic features to it using JavaScript.</p>
<h2 id="heading-how-to-build-a-user-interface-with-html">How to build a user interface with HTML</h2>
<p>In light of React's versatility, we can recreate any site or user interface that we see on the web.</p>
<p>For this lesson, let's remake part of an app that you likely use every day—Google Search.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/09/image-1-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>This may seem ambitious if you are brand new to React, but it requires a knowledge of only two simple concepts: HTML and basic JavaScript functions.</p>
<p>What's the way to build out a user interface without knowing React or even JavaScript?</p>
<p>By using HTML elements as part of a simple HTML document.</p>
<p>Here's how we would show the first three results that come up when you search for "reactjs" in Google.</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>reactjs Search Results<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://reactjs.org"</span>
          &gt;</span>React - A JavaScript Library for Building User Interfaces<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>
        &gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>reactjs.org<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          React makes it painless to create interactive UIs.
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://en.wikipedia.org/wiki/React_(web_framework)"</span>
          &gt;</span>React (web framework) - Wikipedia<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>
        &gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>https://en.wikipedia.org › wiki › React_(web_framework)<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          React is a JavaScript library for building user interfaces.
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://twitter.com/reactjs?lang=en"</span>
          &gt;</span>React (@reactjs) | Twitter<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>
        &gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>https://twitter.com › reactjs<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          The latest Tweets from React (@reactjs).
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Using static HTML alone would be fine if we only needed to show a few links.</p>
<p>But how could we display 100s or 1000s of links this way, all with different data, as a search engine might need to do?</p>
<p>What's a better, that is, a simpler and more extensible way of writing this?</p>
<p>HTML alone is not going to be the answer. We'll need a way of making our site more dynamic to display as many links as we need.</p>
<p>When it comes to adding behavior to a site, we need JavaScript. And since our goal is to build great apps with JavaScript, we know to use React.</p>
<h2 id="heading-how-to-upgrade-any-html-site-to-a-react-app">How to upgrade any HTML site to a React app</h2>
<p>Let's turn our static HTML into a dynamic React app.</p>
<p>Sounds difficult? It's simpler than you think.</p>
<p>We can build a React app out of a single HTML document. All we have to do is bring React in with the following external scripts.*</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/react@16/umd/react.development.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/react-dom@16/umd/react-dom.development.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/babel-standalone@6.26.0/babel.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>The first is for building our React app, and the second is for displaying, or rendering the React app in the browser.</p>
<p>The first is <strong>React</strong>, the second <strong>ReactDOM</strong>.</p>
<p>The third script is to bring in a tool called <strong>Babel</strong>. We'll touch on what that does in a bit.</p>
<p>Here's what our code looks like at this point:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>reactjs Search Results<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/react@16/umd/react.development.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/react-dom@16/umd/react-dom.development.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/babel-standalone@6.26.0/babel.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- our script must have type="text/babel" for Babel to work --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/babel"</span>&gt;</span><span class="javascript">
      <span class="hljs-comment">// React code will go here</span>
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>... and it's almost a React app.</p>
<p>Note that we have a script where we can write our React code, but no HTML.</p>
<p>Let's fix that.</p>
<h2 id="heading-how-to-create-and-render-our-react-app">How to create and render our React app</h2>
<p>Every React app must have what's known as an entry point.</p>
<p>The <strong>entry point</strong> is an HTML element where we insert our React application into the page.</p>
<p>The conventional entry point is a div with the id of root (<code>&lt;div id="root"&gt;&lt;/div&gt;</code>).</p>
<p>We'll add that, then use the <code>render()</code> function from ReactDOM to do the work of rendering the app.</p>
<p>The first is the app itself, meaning our HTML from before, and the second must reference our entry point. We can create that reference by saying <code>document.getElementById('root')</code>.</p>
<p>So now we have everything we need to run our React app:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>reactjs Search Results<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/react@16/umd/react.development.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/react-dom@16/umd/react-dom.development.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/babel-standalone@6.26.0/babel.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"root"</span>&gt;</span>

    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- our script must have type="text/babel" for Babel to work --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/babel"</span>&gt;</span><span class="javascript">
      ReactDOM.render(
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://reactjs.org"</span>
          &gt;</span>React - A JavaScript Library for Building User Interfaces<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>
        &gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>reactjs.org<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          React makes it painless to create interactive UIs.
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://en.wikipedia.org/wiki/React_(web_framework)"</span>&gt;</span>React (web framework) - Wikipedia<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>https://en.wikipedia.org › wiki › React_(web_framework)<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          React is a JavaScript library for building user interfaces.
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://twitter.com/reactjs?lang=en"</span>&gt;</span>React (@reactjs) | Twitter<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>https://twitter.com › reactjs<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          The latest Tweets from React (@reactjs).
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>, document.getElementById('root'))
    </span></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>And if we look at our result, it works like before. Perfect!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/09/image-2-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now let's use React to improve our site by dynamically displaying our links.</p>
<h2 id="heading-how-to-make-html-reusable-with-react-components">How to make HTML reusable with React components</h2>
<p>If we examine our HTML structure, each link consists of the same parts. Each has a URL, a title, a shorter URL, and an excerpt. For each link, we have to repeat the same HTML elements again and again.</p>
<p>In programming, if you're having to repeat yourself a great deal, it's likely a sign that your code can be simplified and shortened in some way. As programmers, we always strive to repeat ourselves as little as possible.</p>
<p>We try to write code that is DRY, where you "don't repeat yourself."</p>
<p>React is, at core, JavaScript plus some features to help us build apps.</p>
<p>And since we're working with JavaScript, what is a JavaScript feature that allows us to create or output something as many times as we like?</p>
<p>A function. </p>
<p>Let's create one here, and we'll call it Link.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Link</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// create link HTML output</span>
}
</code></pre>
<p>The reason being that we want this function to return or output a link's HTML every time we call it.</p>
<p>To do that, let's return our first link's HTML:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Link</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://reactjs.org"</span>&gt;</span>
        React - A JavaScript Library for Building User Interfaces
      <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>reactjs.org<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>React makes it painless to create interactive UIs.<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>So now let's use it to display each link it returns.</p>
<p>To do so, instead of calling it like <code>Link()</code>, in React, we can write it like it was an HTML element <code>&lt;Link /&gt;</code>.</p>
<p>If you've seen this for the first time it might bend your brain a little bit.</p>
<p>Here we are using HTML syntax, but we are calling a JavaScript function! You'll get comfortable with it as you see this more often.</p>
<p>(Also, did you notice that we didn't have to use an opening and closing tag, like it was a normal HTML element? More about that in a minute.)</p>
<p><strong>How does React convert HTML-looking syntax into JavaScript?</strong></p>
<p>It does so with the help of a special tool called Babel, the third script we added. You can see how it works in action here:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/p3550r6dthfd6onee5eg.gif" alt="Babel gif" width="1920" height="1080" loading="lazy"></p>
<p>What's happening?</p>
<p>Babel, a JavaScript tool called a compiler, converts ("compiles") this code that looks like HTML into valid JavaScript.</p>
<h2 id="heading-what-is-this-html-like-syntax-jsx">What is this HTML-like syntax? JSX</h2>
<p>This HTML, which is in fact JavaScript, is called <strong>JSX</strong>, which stands for "JavaScript XML."</p>
<p>XML, if you're not familiar, is a slightly stricter way of writing HTML.</p>
<p>Stricter means that we need to use a closing forward slash for elements with one tag. For example: <code>&lt;input&gt;</code> in HTML as valid JSX is <code>&lt;input /&gt;</code>.</p>
<p>So to reiterate, JSX is <em>not</em> valid JavaScript code.</p>
<p>Meaning, you couldn't put JSX in a browser and expect it to work. We need both a compiler, like Babel, to convert it into valid JavaScript, and then React to use that created JavaScript.</p>
<p>So now to use our custom Link element, we remove all three of the links' HTML and replace them with three Links, like so:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>reactjs Search Results<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/react@16/umd/react.development.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/react-dom@16/umd/react-dom.development.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/babel-standalone@6.26.0/babel.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"root"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/babel"</span>&gt;</span><span class="javascript">
      ReactDOM.render(
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> /&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> /&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span></span>,
        <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>)
      );
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>And if we look at our result, we do indeed have three links.</p>
<p>This is the power of React: it takes the reusability of JavaScript functions, but allows us to use them like they were HTML.</p>
<p>We refer to these custom elements made with JavaScript as <strong>components</strong>.</p>
<p>So we've gained a great deal of readability here by using components. We don't have any confusion about what we're looking at if we've named our components well. No need to read through a ton of HTML elements to see what the app displays.</p>
<p>We see immediately that we have three custom links.</p>
<h2 id="heading-the-anatomy-of-a-function-component">The anatomy of a function component</h2>
<p>Now that we know how components operate, let's take a second look at our Link function component:</p>
<p>Our code may look pretty straightforward, but there are a few subtle things you should take note of here:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Link</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://reactjs.org"</span>&gt;</span>
        React - A JavaScript Library for Building User Interfaces
      <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>reactjs.org<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>React makes it painless to create interactive UIs.<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>The function component name is capitalized: Link instead of link. This is a required naming convention for React components. We use a capitalized name to distinguish components from normal functions. Note that functions which return JSX are not the same as normal JavaScript functions.</p>
<p>Notice how the JSX we are returning has a set of parentheses around it. As you are first writing your React code, it's easy to forget these parentheses, which will result in an error. Wrap your JSX in parentheses if it span more than one line.</p>
<p>Finally, our Link function returns some JSX. Every React component must return either JSX elements or other React components. And yes, React components can return other components.</p>
<p>So since React components can return other React components, we can make an App component.</p>
<p>This App component will contain our entire set or <strong>tree of components</strong> and will show of what our app consists.</p>
<p>And with an App component, this makes our render method much easier to read:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
   ...
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"root"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/babel"</span>&gt;</span><span class="javascript">
      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Link</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">return</span> (
          <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://reactjs.org"</span>&gt;</span>
              React - A JavaScript Library for Building User Interfaces
            <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>reactjs.org<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>React makes it painless to create interactive UIs.<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        );
      }

      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">return</span> (
          <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> /&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span></span>
        );
      }

      ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>));
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>We see from this code that React components have a hierarchy or order like HTML elements. As a result, we can refer to different parts of our component tree as either <strong>parents</strong> or <strong>children</strong>.</p>
<p>In this case, for example, to each rendered Link component, App is the parent. To App, all three Links are its children.</p>
<p>Note that whenever we render or return JSX, there can only be one parent component. But a parent component can have as many child components (as well as elements) as needed.</p>
<p>When we look at the output of our code, you've likely noticed the following problem:</p>
<p>We have three instances of the same link! That's because we are using the same data for each link we create. Yet we know each link has different data—a different title, URL, short URL, and excerpt.</p>
<p>So how do we pass in unique data to our components?</p>
<h2 id="heading-how-to-pass-dynamic-data-to-components-props">How to pass dynamic data to components: Props</h2>
<p>If we wanted to pass some title text to a normal JavaScript function we would do so like this:</p>
<pre><code class="lang-js">Link(<span class="hljs-string">"Our link title here"</span>);
</code></pre>
<p>But how to we do pass data to <strong>function components</strong>?</p>
<p>Normal HTML elements accept data in the form of attributes. But unlike HTML attributes, attributes aren't recognized on React components. The data doesn't stay on the component itself. Where do they go?</p>
<p>As arguments to the function component. Again, this is something we're familiar with since we know the basics of functions.</p>
<p>We know that functions output data using <code>return</code> and accept data with <em>arguments</em>.</p>
<p>If we had an HTML element, say a div with an attribute called "title", this code would be invalid. HTML doesn't have a title attributes for any of its elements:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Our link title here"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>But if we created this title "attribute" on our Link component:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Our link title here"</span> /&gt;</span>
</code></pre>
<p>This is valid. And since we wrote title like an attribute on our component, it is passed to the Link function as an argument called "title".</p>
<p>In code we can think of it like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> linkData = { <span class="hljs-attr">title</span>: <span class="hljs-string">"Our link title here"</span> };

Link(linkData);
</code></pre>
<p>Notice that the linkData argument is an object.</p>
<p>React collects and organizes the data passed to a given component as a single object.</p>
<p>The name for data passed to a component, such as title, is <strong>props</strong>.</p>
<p>All prop values exist within the function component itself on a props object.</p>
<p>And since our goal is to use our title prop within our Link component, we can write the following:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Link</span>(<span class="hljs-params">props</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">a</span>&gt;</span>{props.title}<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span></span>;
}
</code></pre>
<p>We use the curly braces <code>{}</code> syntax to insert the title prop from props.title wherever we want. And the same applies to any other prop passed down to a component.</p>
<p>These curly braces allow us to insert or interpolate dynamic values wherever we need.</p>
<p>Now we have everything we need to fix our links. For each of the Link components, we need to pass down their title, URL, short URL, and excerpt as individual props:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>reactjs Search Results<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/react@16/umd/react.development.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/react-dom@16/umd/react-dom.development.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/babel-standalone@6.26.0/babel.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"root"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/babel"</span>&gt;</span><span class="javascript">
      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Link</span>(<span class="hljs-params">props</span>) </span>{
        <span class="hljs-keyword">return</span> (
          <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">{props.url}</span>&gt;</span>{props.title}<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>{props.shortUrl}<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>{props.excerpt}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        );
      }

      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">return</span> (
          <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Link</span>
              <span class="hljs-attr">title</span>=<span class="hljs-string">"React - A JavaScript Library for Building User Interfaces"</span>
              <span class="hljs-attr">url</span>=<span class="hljs-string">"https://reactjs.org"</span>
              // <span class="hljs-attr">props</span> <span class="hljs-attr">consisting</span> <span class="hljs-attr">of</span> <span class="hljs-attr">two</span> <span class="hljs-attr">or</span> <span class="hljs-attr">more</span> <span class="hljs-attr">words</span> <span class="hljs-attr">must</span> <span class="hljs-attr">be</span> <span class="hljs-attr">written</span> <span class="hljs-attr">in</span> <span class="hljs-attr">camelcase</span>
              <span class="hljs-attr">shortUrl</span>=<span class="hljs-string">"reactjs.org"</span>
              <span class="hljs-attr">excerpt</span>=<span class="hljs-string">"React makes it painless to create interactive UIs."</span>
            /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Link</span>
              <span class="hljs-attr">title</span>=<span class="hljs-string">"React (web framework) - Wikipedia"</span>
              <span class="hljs-attr">url</span>=<span class="hljs-string">"https://en.wikipedia.org/wiki/React_(web_framework)"</span>
              <span class="hljs-attr">shortUrl</span>=<span class="hljs-string">"en.wikipedia.org › wiki › React_(web_framework)"</span>
              <span class="hljs-attr">excerpt</span>=<span class="hljs-string">"React is a JavaScript library for building user interfaces."</span>
            /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Link</span>
              <span class="hljs-attr">title</span>=<span class="hljs-string">"React (@reactjs) | Twitter"</span>
              <span class="hljs-attr">url</span>=<span class="hljs-string">"https://twitter.com/reactjs"</span>
              <span class="hljs-attr">shortUrl</span>=<span class="hljs-string">"twitter.com › reactjs"</span>
              <span class="hljs-attr">excerpt</span>=<span class="hljs-string">"The latest Tweets from React (@reactjs)."</span>
            /&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span></span>
        );
      }

      ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>));
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Looking at our output, we still get the same result.</p>
<p>But there was a bit of a trade-off here. Through props, we were able to make our Link component much more readable.</p>
<p>Now we can make any Link based off of whatever (valid) prop data we give it.</p>
<p>But now you can see that our App component got a lot bigger by providing the prop values immediately on each Link.</p>
<p><em>Isn't there a way that we can move all this data somewhere else?</em></p>
<h2 id="heading-how-to-separate-the-data-from-the-interface">How to separate the data from the interface</h2>
<p>Let's move our data out of the component tree and put it somewhere more convenient, but still use the data as needed.</p>
<p>To do that we'll make an array of objects with the link data to pass down to the Link components through props.</p>
<p>This allows us to put our data wherever we want, in another JavaScript file even. The benefit is that it doesn't clutter up our components anymore.</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    ...
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"root"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/babel"</span>&gt;</span><span class="javascript">
      <span class="hljs-keyword">const</span> linkData = [
        {
          <span class="hljs-attr">title</span>: <span class="hljs-string">"React - A JavaScript Library for Building User Interfaces"</span>,
          <span class="hljs-attr">url</span>: <span class="hljs-string">"https://reactjs.org"</span>,
          <span class="hljs-attr">shortUrl</span>: <span class="hljs-string">"reactjs.org"</span>,
          <span class="hljs-attr">excerpt</span>: <span class="hljs-string">"React makes it painless to create interactive UIs."</span>
        },
        {
          <span class="hljs-attr">title</span>: <span class="hljs-string">"React (web framework) - Wikipedia"</span>,
          <span class="hljs-attr">url</span>: <span class="hljs-string">"https://en.wikipedia.org/wiki/React_(web_framework)"</span>,
          <span class="hljs-attr">shortUrl</span>: <span class="hljs-string">"en.wikipedia.org › wiki › React_(web_framework)"</span>,
          <span class="hljs-attr">excerpt</span>: <span class="hljs-string">"React is a JavaScript library for building user interfaces."</span>
        },
        {
          <span class="hljs-attr">title</span>: <span class="hljs-string">"React (@reactjs) | Twitter"</span>,
          <span class="hljs-attr">url</span>: <span class="hljs-string">"https://twitter.com/reactjs"</span>,
          <span class="hljs-attr">shortUrl</span>: <span class="hljs-string">"twitter.com › reactjs"</span>,
          <span class="hljs-attr">excerpt</span>: <span class="hljs-string">"The latest Tweets from React (@reactjs)."</span>
        }
      ];

      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Link</span>(<span class="hljs-params">props</span>) </span>{
        <span class="hljs-keyword">return</span> (
          <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">{props.url}</span>&gt;</span>{props.title}<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>{props.shortUrl}<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>{props.excerpt}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        );
      }

      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">return</span> (
          <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">title</span>=<span class="hljs-string">""</span> <span class="hljs-attr">url</span>=<span class="hljs-string">""</span> <span class="hljs-attr">shortUrl</span>=<span class="hljs-string">""</span> <span class="hljs-attr">excerpt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">title</span>=<span class="hljs-string">""</span> <span class="hljs-attr">url</span>=<span class="hljs-string">""</span> <span class="hljs-attr">shortUrl</span>=<span class="hljs-string">""</span> <span class="hljs-attr">excerpt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">title</span>=<span class="hljs-string">""</span> <span class="hljs-attr">url</span>=<span class="hljs-string">""</span> <span class="hljs-attr">shortUrl</span>=<span class="hljs-string">""</span> <span class="hljs-attr">excerpt</span>=<span class="hljs-string">""</span> /&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span></span>
        );
      }

      ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>));
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Now how do we display each Link with its data using the linkData array?</p>
<p>If you've worked with arrays before, to get each element we loop or iterate over the array. Here, for each loop, we can pass the props data down to the Link component again.</p>
<p>This pattern is a very common one in React. So much so that there is a special array method that we can use to perform this iteration, called .map(). It is not the same as the map method in regular JavaScript—it is for working with JSX and components alone.</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    ...
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"root"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/babel"</span>&gt;</span><span class="javascript">
      <span class="hljs-keyword">const</span> linkData = [
        {
          <span class="hljs-attr">title</span>: <span class="hljs-string">"React - A JavaScript Library for Building User Interfaces"</span>,
          <span class="hljs-attr">url</span>: <span class="hljs-string">"https://reactjs.org"</span>,
          <span class="hljs-attr">shortUrl</span>: <span class="hljs-string">"reactjs.org"</span>,
          <span class="hljs-attr">excerpt</span>: <span class="hljs-string">"React makes it painless to create interactive UIs."</span>
        },
        {
          <span class="hljs-attr">title</span>: <span class="hljs-string">"React (web framework) - Wikipedia"</span>,
          <span class="hljs-attr">url</span>: <span class="hljs-string">"https://en.wikipedia.org/wiki/React_(web_framework)"</span>,
          <span class="hljs-attr">shortUrl</span>: <span class="hljs-string">"en.wikipedia.org › wiki › React_(web_framework)"</span>,
          <span class="hljs-attr">excerpt</span>: <span class="hljs-string">"React is a JavaScript library for building user interfaces."</span>
        },
        {
          <span class="hljs-attr">title</span>: <span class="hljs-string">"React (@reactjs) | Twitter"</span>,
          <span class="hljs-attr">url</span>: <span class="hljs-string">"https://twitter.com/reactjs"</span>,
          <span class="hljs-attr">shortUrl</span>: <span class="hljs-string">"twitter.com › reactjs"</span>,
          <span class="hljs-attr">excerpt</span>: <span class="hljs-string">"The latest Tweets from React (@reactjs)."</span>
        }
      ];

      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Link</span>(<span class="hljs-params">props</span>) </span>{
        <span class="hljs-keyword">return</span> (
          <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">{props.url}</span>&gt;</span>{props.title}<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>{props.shortUrl}<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>{props.excerpt}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        );
      }

      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">return</span> (
          <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
            {linkData.map(function(link) {
              return (
                <span class="hljs-tag">&lt;<span class="hljs-name">Link</span>
                  <span class="hljs-attr">key</span>=<span class="hljs-string">{link.url}</span>
                  <span class="hljs-attr">title</span>=<span class="hljs-string">{link.title}</span>
                  <span class="hljs-attr">url</span>=<span class="hljs-string">{link.url}</span>
                  <span class="hljs-attr">shortUrl</span>=<span class="hljs-string">{link.shortUrl}</span>
                  <span class="hljs-attr">excerpt</span>=<span class="hljs-string">{link.excerpt}</span>
                /&gt;</span>
              );
            })}
          <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span></span>
        );
      }

      ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>));
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>By moving our data out of the UI and displaying each link using .map(), we have a far simpler React app that can accept as many links as we choose.</p>
<p>Finally, note in our code that where we are mapping over our linkData, the entire expression is surrounded by our curly braces. Be aware that JSX allows us to insert any valid JavaScript expression between curly braces.</p>
<h2 id="heading-how-to-build-apps-the-react-way">How to build apps the "React" way</h2>
<p>What was the point of covering these various patterns?</p>
<p>Not only to cover the basics of JSX and how React combines HTML and JavaScript, but also to show you how React developers think.</p>
<p>How do you think like a React developer? By knowing how to break down your UI into reusable components.</p>
<p>When a React developer plans out an application they want to make, they start by identifying all individual parts of the app and seeing what parts can be made into reusable components. </p>
<p>We do that by seeing if each part has the same visual (HTML) structures and accept the same or very similar sets of data (through props).</p>
<p>Now to come full circle, let's take a new look at the starting UI that we wanted to recreate at the beginning. </p>
<p>If we were to look at this like a React developer, it might look something like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/09/image-3-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The better you get with using components, the faster you'll be able to build your own websites and applications with ease.</p>
<h2 id="heading-become-a-professional-react-developer">Become a Professional React Developer</h2>
<p>React is hard. You shouldn't have to figure it out yourself.</p>
<p>I've put everything I know about React into a single course, to help you reach your goals in record time:</p>
<p><a target="_blank" href="https://www.reactbootcamp.com"><strong>Introducing: The React Bootcamp</strong></a></p>
<p><strong>It’s the one course I wish I had when I started learning React.</strong></p>
<p>Click below to try the React Bootcamp for yourself:</p>
<p><a target="_blank" href="https://www.reactbootcamp.com"><img src="https://reedbarger.nyc3.digitaloceanspaces.com/reactbootcamp/react-bootcamp-cta-alt.png" alt="Click to join the React Bootcamp" width="600" height="400" loading="lazy"></a>
<em>Click to get started</em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JSX in React – Explained with Examples ]]>
                </title>
                <description>
                    <![CDATA[ JSX is one of the core concepts of React. So if you understand it well, you'll be able to write better React code. In this article, we'll explore: What is JSX in React and how to use it How JSX is transformed to React.createElement What is a JSX exp... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/jsx-in-react-introduction/</link>
                <guid isPermaLink="false">66bc552495b2a9e41d4e9687</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JSX ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Yogesh Chavan ]]>
                </dc:creator>
                <pubDate>Mon, 01 Feb 2021 21:24:17 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/01/jsx.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>JSX is one of the core concepts of React. So if you understand it well, you'll be able to write better React code.</p>
<p>In this article, we'll explore:</p>
<ul>
<li>What is JSX in React and how to use it</li>
<li>How JSX is transformed to <code>React.createElement</code></li>
<li>What is a JSX expression and what we can write inside it</li>
<li>Common issues in JSX </li>
</ul>
<p>And much more. So let's get started.</p>
<h2 id="heading-what-is-jsx">What is JSX?</h2>
<blockquote>
<p>JSX is a JavaScript Extension Syntax used in React to easily write HTML and JavaScript together.</p>
</blockquote>
<p>Take a look at the below code:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> jsx = <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>This is JSX<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>
</code></pre>
<p>This is simple JSX code in React. But the browser does not understand this JSX because it's not valid JavaScript code. This is because we're assigning an HTML tag to a variable that is not a string but just HTML code.</p>
<p>So to convert it to browser understandable JavaScript code, we use a tool like <a target="_blank" href="https://babeljs.io/">Babel</a> which is a JavaScript compiler/transpiler.</p>
<p>You can set up your own babel configuration using Webpack as I show in <a target="_blank" href="https://medium.com/javascript-in-plain-english/webpack-and-babel-setup-with-react-from-scratch-bef0fe2ae3e7?source=friends_link&amp;sk=880a6b9a35fb638eef19e5e99276428e">this article</a>. Or you can use <a target="_blank" href="https://github.com/facebook/create-react-app">create-react-app</a> which internally uses Babel for the JSX to JavaScript conversion.</p>
<p>We can use the above JSX in our React code like this:</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXDemo</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
    render() {
        <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>This is JSX<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>;
    }
}

ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">JSXDemo</span> /&gt;</span></span>, <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>));
</code></pre>
<p>Here, we're returning the JSX from the <code>JSXDemo</code> component and rendering that on the screen using the <code>ReactDOM.render</code> method.</p>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/awesome-framework-7kr3d?file=/src/index.js">Code Sandbox Demo</a>.</p>
<p>When the Babel executes the above JSX, it converts it to the following code:</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXDemo</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
    render() {
        <span class="hljs-keyword">return</span> React.createElement(<span class="hljs-string">"h1"</span>, <span class="hljs-literal">null</span>, <span class="hljs-string">"This is JSX"</span>);
    }
}
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/billowing-dust-b357d?file=/src/index.js">Code Sandbox Demo</a>.</p>
<p>As you can see in the above Code Sandbox, the code still correctly prints the contents to the screen using <code>React.createElement</code>.</p>
<p>This was the old way of writing code in React – but it's tedious to write the <code>React.createElement</code> every time, even for adding a simple div.</p>
<p>So React introduced the JSX way of writing code which makes code easy to write and understand.</p>
<blockquote>
<p>Knowing how to convert JSX to <code>React.createElement</code> is very important as a React developer (it's also a popular interview question).</p>
</blockquote>
<h2 id="heading-what-is-the-reactcreateelement-function">What is the React.createElement Function?</h2>
<p>Every JSX is converted to the <code>React.createElement</code> function call that the browser understands.</p>
<p>The <code>React.createElement</code> has the following syntax:</p>
<pre><code class="lang-js">React.createElement(type, [props], [...children])
</code></pre>
<p>Let’s look at the parameters of the <code>createElement</code> function.</p>
<ul>
<li><strong>type</strong> can be an HTML tag like h1, div or it can be a React component</li>
<li><strong>props</strong> are the attributes you want the element to have</li>
<li><strong>children</strong> contain other HTML tags or can be a component</li>
</ul>
<p>The <code>React.createElement</code> call will also be converted to the object representation like this:</p>
<pre><code class="lang-js">{   
 <span class="hljs-attr">type</span>: <span class="hljs-string">'h1'</span>,   
 <span class="hljs-attr">props</span>: {     
   <span class="hljs-attr">children</span>: <span class="hljs-string">'This is JSX'</span>   
 }
}
</code></pre>
<p>You can see this object representation if you assign the JSX to some local variable and log it as shown below:</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXDemo</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
    render() {
        <span class="hljs-keyword">const</span> jsx = <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>This is JSX<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>;
        <span class="hljs-built_in">console</span>.log(jsx);
        <span class="hljs-keyword">return</span> jsx;
    }
}

ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">JSXDemo</span> /&gt;</span></span>, <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>));
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/epic-spence-jcp5t?file=/src/index.js">Code Sandbox Demo</a>.</p>
<p>You will see the log printed as shown below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/log.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now, take a look at the below code:</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXDemo</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">const</span> jsx = <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"jsx"</span>&gt;</span>This is JSX<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>;
    <span class="hljs-built_in">console</span>.log(jsx);
    <span class="hljs-keyword">return</span> jsx;
  }
}

ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">JSXDemo</span> /&gt;</span></span>, <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>));
</code></pre>
<p>Here, we've used the JSX like this:</p>
<pre><code class="lang-js">&lt;h1 id=<span class="hljs-string">"jsx"</span>&gt;This is JSX&lt;/h1&gt;
</code></pre>
<p>So React, will convert this JSX to the below code:</p>
<pre><code class="lang-js">React.createElement(<span class="hljs-string">"h1"</span>, { <span class="hljs-attr">id</span>: <span class="hljs-string">"jsx"</span> }, <span class="hljs-string">"This is JSX"</span>);
</code></pre>
<p>If there are any attributes added to the HTML tag as in our case, they will be passed as the second parameter for the <code>React.createElement</code> call. The object representation will look like this:</p>
<pre><code class="lang-js">{ 
  <span class="hljs-attr">type</span>: <span class="hljs-string">'h1'</span>, 
  <span class="hljs-attr">props</span>: { 
   <span class="hljs-attr">id</span>: <span class="hljs-string">'jsx'</span>,
   <span class="hljs-attr">children</span>: <span class="hljs-string">'This is JSX'</span>
  } 
}
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/infallible-lake-rz7vj?file=/src/index.js">Code Sandbox Demo</a>.</p>
<p>You will see the log printed as shown below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/create_element.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now, let's add some complexity to the JSX to see how it's converted to the <code>React.createElement</code> call.</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXDemo</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  handleOnClick = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"clicked"</span>);
  };
  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"btn"</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.handleOnClick}</span>&gt;</span>
        Click Here
      <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
    );
  }
}

ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">JSXDemo</span> /&gt;</span></span>, <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>));
</code></pre>
<p>Here, we've added an <code>onClick</code> handler to the button.</p>
<p>For the above code, the <code>React.createElement</code> call will look like this:</p>
<pre><code class="lang-js">React.createElement(<span class="hljs-string">"button"</span>, {
  <span class="hljs-attr">id</span>: <span class="hljs-string">"btn"</span>, 
  <span class="hljs-attr">onClick</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{}
}, <span class="hljs-string">"Click Here"</span>)
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/new-dew-sc2rp?file=/src/index.js">Code Sandbox Demo</a>.</p>
<p>The object representation will look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/id_children.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>So from all the above examples, it's clear that JSX is converted to a <code>React.createElement</code> call and it's then converted to its object representation.</p>
<p>If you want to see the JSX to <code>React.createElement</code> conversion code, you can navigate to <a target="_blank" href="https://babel-repl-clone.now.sh/">this application</a> which I created in <a target="_blank" href="https://levelup.gitconnected.com/create-a-clone-of-babel-repl-site-to-convert-es6-react-code-to-es5-93cdc9ad98ea?source=friends_link&amp;sk=517cfac3dfc4b451610eb298f36a428c">this article</a>. There you can write JSX code on left and see the converted code on the right side as shown below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/conversion.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-return-complex-jsx">How to Return Complex JSX</h2>
<p>Take a look at the below code:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">"react-dom"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is first JSX Element!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is another JSX Element<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">const</span> rootElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>);
ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, rootElement);
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/objective-thunder-3hqqz?file=/src/index.js">Code Sandbox demo</a>.</p>
<p>Here, we're returning two paragraphs from the App component. But if you run the code, you will get this error:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/adjacent_error.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>We're getting an error because React requires adjacent elements to be wrapped in a parent tag.</p>
<p>As we have seen, <code>&lt;p&gt;This is first JSX Element!&lt;/p&gt;</code> will be converted to <code>React.createElement("p", null, "This is first JSX Element!")</code> and <code>&lt;p&gt;This is another JSX Element&lt;/p&gt;</code> will be converted to <code>React.createElement("p", null, "This is another JSX Element")</code>.</p>
<p>The converted code will look like this now:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">"react-dom"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
            React.createElement(<span class="hljs-string">"p"</span>, <span class="hljs-literal">null</span>, <span class="hljs-string">"This is first JSX Element!"</span>); React.createElement(<span class="hljs-string">"p"</span>, <span class="hljs-literal">null</span>, <span class="hljs-string">"This is another JSX Element"</span>);
        );
};

<span class="hljs-keyword">const</span> rootElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>);
ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, rootElement);
</code></pre>
<p>Here we are returning two things from the <code>App</code> component which will not work because there is no parent element to wrap both of them.</p>
<p>To make it work, the obvious solution is to wrap both of them in some parent element, most probably a <code>div</code> like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">"react-dom"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is first JSX Element!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is another JSX Element<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">const</span> rootElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>);
ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, rootElement);
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/stoic-khorana-vnrt6?file=/src/index.js">Code Sandbox Demo</a>.</p>
<p>But there are also other ways of making it work.</p>
<p>First, you can try returning it as an array as shown below:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">"react-dom"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    [<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is first JSX Element!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>,<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is another JSX Element<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>]
  );
};

<span class="hljs-keyword">const</span> rootElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>);
ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, rootElement);
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/crazy-banach-wy756?file=/src/index.js">Code Sandbox Demo</a>.</p>
<p>This will get the job done, but as you can see in the browser console, you will get a warning saying <code>Warning: Each child in a list should have a unique "key" prop.</code></p>
<blockquote>
<p>Because in React, every element in the array (when displayed using JSX) needs to have a unique key added to it.</p>
</blockquote>
<p>We can fix it by adding a unique key for the adjacent elements:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">"react-dom"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    [<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">key</span>=<span class="hljs-string">"first"</span>&gt;</span>This is first JSX Element!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>,<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">key</span>=<span class="hljs-string">"second"</span>&gt;</span>This is another JSX Element<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>]
  );
};

<span class="hljs-keyword">const</span> rootElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>);
ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, rootElement);
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/relaxed-resonance-ljzzf?file=/src/index.js">Code Sandbox Demo</a>.</p>
<p>The other way to fix it is by using the <code>React.Fragment</code> component:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">"react-dom"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.Fragment</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is first JSX Element!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is another JSX Element<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">React.Fragment</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">const</span> rootElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>);
ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, rootElement);
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/fervent-morse-gsvk8?file=/src/index.js">Code Sandbox Demo</a>.</p>
<p><code>React.Fragment</code> was added in React version 16.2 because we always have to wrap multiple adjacent elements in some tag (like div) inside every JSX returned by a component. But that adds unnecessary div tags.</p>
<p>This is fine most of the time but there are certain cases where it's not fine.</p>
<p>For example, if we're using Flexbox, then there is a special parent-child relationship in Flexbox's structure. And adding divs in the middle makes it hard to keep the desired layout.</p>
<p>So using <code>React.Fragment</code> fixes this issue.</p>
<blockquote>
<p><em>Fragments</em> let you group a list of children without adding extra nodes to the DOM.</p>
</blockquote>
<h2 id="heading-how-to-add-comments-to-jsx-code">How to Add Comments to JSX Code</h2>
<p>If you have a line of code like this:</p>
<pre><code class="lang-js">&lt;p&gt;This is some text&lt;/p&gt;
</code></pre>
<p>and you want to add a comment for that code, then you have to wrap that code in JSX expresssion syntax inside the <code>/*</code> and <code>*/</code> comment symbols like this:</p>
<pre><code class="lang-js">{<span class="hljs-comment">/* &lt;p&gt;This is some text&lt;/p&gt; */</span>}
</code></pre>
<p><em>Tip:</em> Instead of manually typing the comment, you can use <code>Cmd + /</code> (Mac) or <code>Ctrl + /</code> shortcut keys to add or remove the comment.</p>
<h2 id="heading-how-to-add-javascript-code-in-jsx">How to Add JavaScript Code in JSX</h2>
<p>Up to this point, we have only used HTML tags as a part of JSX. But JSX becomes more useful when we actually add JavaScript code inside it.</p>
<p>To add JavaScript code inside JSX, we need to write it in curly brackets like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
 <span class="hljs-keyword">const</span> number = <span class="hljs-number">10</span>;
 <span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Number: {number}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
 );
};
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/keen-leftpad-jygwo?file=/src/index.js">Code Sandbox Demo</a>.</p>
<blockquote>
<p>Inside the curly brackets we can only write an expression that evaluates to some value.</p>
</blockquote>
<p>So, often this syntax of using curly brackets is known as JSX Expression Syntax.</p>
<p>Following are the valid things you can have in a JSX Expression:</p>
<ul>
<li>A string like "hello" </li>
<li>A number like 10</li>
<li>An array like [1, 2, 4, 5]</li>
<li>An object property that will evaluate to some value</li>
<li>A function call that returns some value which may be JSX</li>
<li>A map method that always returns a new array</li>
<li>JSX itself</li>
</ul>
<p>Following are the invalid things and cannot be used in a JSX Expression:</p>
<ul>
<li>A for loop or while loop or any other loop</li>
<li>A variable declaration</li>
<li>A function declaration</li>
<li>An if condition</li>
<li>An object</li>
</ul>
<p>We can write arrays in JSX Expressions because <code>&lt;p&gt;{[1, 2, 3, 4]}&lt;/p&gt;</code> is finally converted to <code>&lt;p&gt;{1}{2}{3}{4}&lt;/p&gt;</code> when rendering (which can be rendered without any issue).</p>
<p>In the case of an object, it’s not clear how the object should be displayed. For example, should it be comma-separated key-value pairs or should it be displayed as JSON? So you will get an error if you try to display the object in a JSX expression. But we can use object properties instead.</p>
<blockquote>
<p>Also note that undefined, null, and boolean are not displayed on the UI when used inside JSX.</p>
</blockquote>
<p>So if you have a boolean value and you want to display it on the UI you need to wrap it in ES6 template literal syntax like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> isAdmin = <span class="hljs-literal">true</span>;
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>isAdmin is {`${isAdmin}`} <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/ecstatic-shamir-7b5z6?file=/src/index.js">Code Sandbox Demo</a>.</p>
<h3 id="heading-conditional-operators-in-jsx-expressions">Conditional Operators in JSX Expressions</h3>
<p>We can’t write <em>if conditions</em> in JSX expressions, which you might think of as an issue. But React allows us to write conditional operators, like ternary operators as well as the logical short circuit &amp;&amp; operator like this:</p>
<pre><code class="lang-js">&lt;p&gt;{a &gt; b ? <span class="hljs-string">"Greater"</span> : <span class="hljs-string">"Smaller"</span>}&lt;/p&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{shouldShow &amp;&amp; "Shown"}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/condescending-wind-4rwtl">Code Sandbox Demo</a> describing various ways of writing JSX expressions.</p>
<h2 id="heading-how-to-nest-jsx-expressions">How to Nest JSX Expressions</h2>
<p>You can even do nesting of JSX expressions like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> number = <span class="hljs-number">10</span>;
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      {number &gt; 0 ? (
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Number {number} is positive<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      ) : (
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Number {number} is Negative<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      )}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/frosty-dew-mj351?file=/src/index.js">Code Sandbox Demo</a>.</p>
<h2 id="heading-how-to-add-a-class-in-jsx">How to Add a Class in JSX</h2>
<p>We can add attributes to the JSX elements, for example <code>id</code> and <code>class</code>, the same as in HTML.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">"react-dom"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> id = <span class="hljs-string">"some-id"</span>;
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">id</span>=<span class="hljs-string">{id}</span>&gt;</span>This is a heading<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"active"</span>&gt;</span>This is another heading<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">const</span> rootElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>);
ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, rootElement);
</code></pre>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/great-chandrasekhar-i48t2">Code Sandbox Demo</a>.</p>
<p>Note that in React, we need to use <code>className</code> instead of <code>class</code>.</p>
<p>This is because if you use <code>class</code> instead of <code>className</code>, you will get a warning in the console as shown below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/class_warning.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/happy-smoke-ecbtl?file=/src/index.js">Code Sandbox Demo</a>.</p>
<p>To understand why the warning is being shown, print the object representation of it and you will see the following:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/class_info-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Here's a <a target="_blank" href="https://codesandbox.io/s/epic-frost-e64ll?file=/src/index.js">Code Sandbox Demo</a>.</p>
<p>As you can see, the props object has the <code>class</code> property with a value <code>active</code>. But in JavaScript, <code>class</code> is a reserved keyword so accessing <code>props.class</code> will result in an error.</p>
<p>This is why React decided to use <code>className</code> instead of <code>class</code>.</p>
<p>This use of <code>className</code> instead of <code>class</code> is a frequently asked question in React interviews.</p>
<blockquote>
<p>Note that in React, all the attribute names are written in camelCase.</p>
</blockquote>
<p>You can find all the changed and unchanged attributes list <a target="_blank" href="https://reactjs.org/docs/dom-elements.html#all-supported-html-attributes">here</a>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, we have seen how to use JSX in React. Here are some major takeaways:</p>
<ul>
<li>Every JSX tag is converted to <code>React.createElement</code> call and its object representation.</li>
<li>JSX Expressions, which are written inside curly brackets, allow only things that evaluate to some value like string, number, array map method and so on.</li>
<li>In React, we have to use <code>className</code> instead of <code>class</code> for adding classes to the HTML element </li>
<li>All attribute names in React are written in camelCase.</li>
<li><code>undefined</code>, <code>null</code>, and <code>boolean</code> are not displayed on the UI when used inside JSX.</li>
</ul>
<h3 id="heading-thanks-for-reading">Thanks for reading!</h3>
<p>Check out my free <a target="_blank" href="https://yogeshchavan.podia.com/react-router-introduction">Introduction to React Router</a> course.</p>
<p>Also, check out my <a target="_blank" href="https://modernjavascript.yogeshchavan.dev/">Mastering Modern JavaScript</a> book to learn all the latest ES6+ features in detail to become better at JavaScript and React.</p>
<p><strong>Subscribe to my <a target="_blank" href="https://yogeshchavan.dev/">weekly newsletter</a> to join 1000+ other subscribers to get amazing tips, tricks, articles and discount deals directly in your inbox.</strong></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ React Functional Components, Props, and JSX – React.js Tutorial for Beginners ]]>
                </title>
                <description>
                    <![CDATA[ By Cem Eygi React is one of the most popular JavaScript libraries for building user interfaces.  If you want to become a front-end developer or find a web development job, you would probably benefit from learning React in-depth. In this post, you're ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-components-jsx-props-for-beginners/</link>
                <guid isPermaLink="false">66d45df836c45a88f96b7cd1</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JSX ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 05 Nov 2020 20:59:58 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/11/Copy-of-Add-a-heading.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Cem Eygi</p>
<p>React is one of the most popular JavaScript libraries for building user interfaces. </p>
<p>If you want to become a front-end developer or find a web development job, you would probably benefit from learning React in-depth.</p>
<p>In this post, you're going to learn some of the basics of React like creating a component, the JSX syntax, and Props. If you have no or little experience with React, this post is for you.</p>
<p>For starters, <a target="_blank" href="https://www.freecodecamp.org/news/install-react-with-create-react-app/">here's how you can install React</a>.</p>
<h2 id="heading-what-is-jsx">What is JSX?</h2>
<p>The first thing you'll realize after installing your first React project is that a JavaScript function returns some HTML code:</p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">header</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-header"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{logo}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-logo"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"logo"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
          Edit <span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>src/App.js<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span> and save to reload.
        <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>This is a special and valid syntax extension for React which is called JSX (JavaScript XML). Normally in frontend-related projects, we keep HTML, CSS, and JavaScript code in separate files. However in React, this works a bit differently.</p>
<p>In React projects, we don't create separate HTML files, because JSX allows us to write HTML and JavaScript combined together in the same file, like in the example above. You can, however, separate your CSS in another file.</p>
<p>In the beginning, JSX might seem a little bit weird. But don't worry, you'll get used to it. </p>
<p>JSX is very practical, because we can also execute any JavaScript code (logic, functions, variables, and so on) inside the HTML directly by using curly braces { }, like this:</p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> text = <span class="hljs-string">'Hello World'</span>;

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span> {text} <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>Also, you can assign HTML tags to JavaScript variables:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> message = <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>React is cool!<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>;
</code></pre>
<p>Or you can return HTML inside JavaScript logic (such as if-else cases):</p>
<pre><code class="lang-jsx">render() {
    <span class="hljs-keyword">if</span>(<span class="hljs-literal">true</span>) {
        <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>YES<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>;
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>NO<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>;
    }
}
</code></pre>
<p>I won't go into further details of JSX, but make sure that you consider the following rules while writing JSX:</p>
<ul>
<li>HTML and component tags must always be closed &lt; /&gt;</li>
<li>Some attributes like <strong>“class”</strong> become <strong>“className”</strong> (because class refers to JavaScript classes), <strong>“tabindex”</strong> becomes <strong>“tabIndex”</strong> and should be written camelCase</li>
<li>We can’t return more than one HTML element at once, so make sure to wrap them inside a parent tag:</li>
</ul>
<pre><code class="lang-jsx"><span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Hello<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>World<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
);
</code></pre>
<ul>
<li>or as an alternative, you can wrap them with empty tags:</li>
</ul>
<pre><code class="lang-jsx"><span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Hello<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>World<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/&gt;</span></span>
);
</code></pre>
<p>You can also watch my React for Beginners tutorial for more info:</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/QJZ-xgt4SJo" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h2 id="heading-what-are-functional-amp-class-components">What are Functional &amp; Class Components?</h2>
<p>After getting used to the JSX syntax, the next thing to understand is the component-based structure of React. </p>
<p>If you revisit the example code at the top of this post, you'll see that the JSX code is being returned by a function. But the App( ) function is not an ordinary function – it is actually a component. So what is a component?</p>
<h3 id="heading-what-is-a-component">What is a Component?</h3>
<p>A component is an independent, reusable code block which divides the UI into smaller pieces. For example, if we were building the UI of Twitter with React:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/11/twit.png" alt="Image" width="600" height="400" loading="lazy">
<em>The components of Twitter's News Feed</em></p>
<p>Rather than building the whole UI under one single file, we can and we should divide all the sections (marked with red) into smaller independent pieces. In other words, these are components.</p>
<p>React has two types of components: functional and class. Let's look at each now in more detail.</p>
<h3 id="heading-functional-components">Functional Components</h3>
<p>The first and recommended component type in React is functional components. A functional component is basically a JavaScript/ES6 function that returns a React element (JSX). According to React's official docs, the function below is a valid functional component:</p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Welcome</span>(<span class="hljs-params">props</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello, {props.name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>;
}
</code></pre>
<p>Alternatively, you can also create a functional component with the arrow function definition:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> Welcome = <span class="hljs-function">(<span class="hljs-params">props</span>) =&gt;</span> { 
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello, {props.name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>; 
}
</code></pre>
<blockquote>
<p>This function is a valid React component because it accepts a single “props” (which stands for properties) object argument with data and returns a React element. — <a target="_blank" href="https://reactjs.org/"><strong>reactjs.org</strong></a></p>
</blockquote>
<p>To be able to use a component later, you need to first export it so you can import it somewhere else:</p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Welcome</span>(<span class="hljs-params">props</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello, {props.name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>;
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Welcome;
</code></pre>
<p>After importing it, you can call the component like in this example:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> Welcome <span class="hljs-keyword">from</span> <span class="hljs-string">'./Welcome'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{ 
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Welcome</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p> So a Functional Component in React:</p>
<ul>
<li>is a JavaScript/ES6 function</li>
<li>must return a React element (JSX)</li>
<li>always starts with a capital letter (naming convention)</li>
<li>takes props as a parameter if necessary</li>
</ul>
<h3 id="heading-what-are-class-components">What are Class Components?</h3>
<p>The second type of component is the class component. Class components are ES6 classes that return JSX. Below, you see our same Welcome function, this time as a class component:</p>
<pre><code class="lang-jsx"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Welcome</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello, {this.props.name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>;
  }
}
</code></pre>
<p>Different from functional components, class components must have an additional render( ) method for returning JSX.</p>
<h3 id="heading-why-use-class-components">Why Use Class Components?</h3>
<p>We used to use class components because of "state". In the older versions of React (version &lt; 16.8), it was not possible to use state inside functional components.</p>
<p>Therefore, we needed functional components for rendering UI only, whereas we'd use class components for data management and some additional operations (like life-cycle methods). </p>
<p>This has changed with the introduction of React Hooks, and now we can also use states in functional components as well. (I will be covering state and hooks in my following posts, so don't mind them for now).</p>
<p>A Class Component:</p>
<ul>
<li>is an ES6 class, will be a component once it ‘extends’ a React component.</li>
<li>takes Props (in the constructor) if needed</li>
<li>must have a render( ) method for returning JSX</li>
</ul>
<h2 id="heading-what-are-props-in-react">What are Props in React?</h2>
<p>Another important concept of components is how they communicate. React has a special object called a prop (stands for property) which we use to transport data from one component to another.</p>
<p>But be careful – props only transport data in a one-way flow (only from parent to child components). It is not possible with props to pass data from child to parent, or to components at the same level.</p>
<p>Let's revisit the App( ) function above to see how to pass data with props. </p>
<p>First, we need to define a prop on the Welcome Component and assign a value to it:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> Welcome <span class="hljs-keyword">from</span> <span class="hljs-string">'./Welcome'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{ 
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Welcome</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"John"</span>/&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Welcome</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"Mary"</span>/&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Welcome</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"Alex"</span>/&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>Props are custom values and they also make components more dynamic. Since the Welcome component is the child here, we need to define props on its parent (App), so we can pass the values and get the result simply by accessing the prop "name":</p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Welcome</span>(<span class="hljs-params">props</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello, {props.name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>;
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/11/props.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-react-props-are-really-useful">React Props Are Really Useful</h3>
<p>So React developers use props to pass data and they're useful for this job. But what about managing data? Props are used for passing data, not for manipulating it. I'm going to cover managing data with React in my future posts here on freeCodeCamp.</p>
<p>In the meantime, if you want to learn more about React &amp; Web development, feel free to <a target="_blank" href="https://www.youtube.com/channel/UC1EgYPCvKCXFn8HlpoJwY3Q">subscribe to my YouTube channel</a>.</p>
<p>Thank you for reading!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
