<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Honour</title>
    <description>The latest articles on DEV Community by Honour (@chukshon).</description>
    <link>https://dev.to/chukshon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F849707%2Fc9c23f57-4f4b-4188-80ab-63b04c104afb.png</url>
      <title>DEV Community: Honour</title>
      <link>https://dev.to/chukshon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chukshon"/>
    <language>en</language>
    <item>
      <title>Understanding Functional Component and Class Component</title>
      <dc:creator>Honour</dc:creator>
      <pubDate>Wed, 04 May 2022 14:01:52 +0000</pubDate>
      <link>https://dev.to/chukshon/understanding-functional-component-and-class-component-4ak6</link>
      <guid>https://dev.to/chukshon/understanding-functional-component-and-class-component-4ak6</guid>
      <description>&lt;h3&gt;
  
  
  What are Components
&lt;/h3&gt;

&lt;p&gt;Components are the building blocks of any react app, they allow you to split the ui into smaller pieces. Rather than building the whole ui inside a file, the ui can be split into different files otherwise called Components. This small pieces of ui can be reused and handled independently.&lt;/p&gt;

&lt;p&gt;It is a combination of :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Template using html&lt;/li&gt;
&lt;li&gt;User interactivity using Js&lt;/li&gt;
&lt;li&gt;Applying styles using css&lt;/li&gt;
&lt;li&gt;Types of Components in react&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;React has two types of components, functional(stateless component) and class(stateful). We’ll be looking at each type of component below&lt;/p&gt;

&lt;h2&gt;
  
  
  Functional(stateless) Component
&lt;/h2&gt;

&lt;p&gt;A Functional component is simply a javascript function that accepts inputs which are properties(props) and returns a react element i.e JSX that specifies how a section of the user interface should appear.&lt;/p&gt;

&lt;p&gt;Let us define a simple JavaScript function called Hello() which returns the How Are you JSX code. To pass this JSX into ReactDOM.render() we need to pass our Hello() function as  into ReactDOM.render(). This has the same effect as above where the JSX is passed directly into ReactDOM.render() and gets rendered in the browser.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// This is a functional component
function Greeting() {
  return &amp;lt;h1&amp;gt;How Are you&amp;lt;/h1&amp;gt;;
}

ReactDOM.render(&amp;lt;Greeting/&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above can be rewritten using the es6 Arrow function expression as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// This is a functional component
const Greeting =&amp;gt; () {
  return &amp;lt;h1&amp;gt;How Are you&amp;lt;/h1&amp;gt;;
}

ReactDOM.render(&amp;lt;Greeting/&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Components written as such is referred to as functional(stateless) components.&lt;/p&gt;

&lt;p&gt;Class(stateful) Component&lt;br&gt;
Class component can be described as an ES6 class. The class component has a state and a lifecycle. In the older versions of React (version &amp;lt; 16.8), it was not possible to use state inside functional components. Therefore, functional components was majorly used for rendering UI only, whereas we’d use class components for data management and some additional operations (like life-cycle methods). With the introduction of React Hooks, and now we can also use states in functional components as well.&lt;/p&gt;

&lt;p&gt;A Class Component:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;is an ES6 class, will be a component once it ‘extends’ a React component.&lt;/li&gt;
&lt;li&gt;takes Props (in the constructor) if needed&lt;/li&gt;
&lt;li&gt;must have a render( ) method for returning JSX&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Hello extends React.Component {
  render() {
    return &amp;lt;h1&amp;gt;Hello, World!&amp;lt;/h1&amp;gt;;
  }
}

ReactDOM.render(&amp;lt;Hello/&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the above explanation i explained functional components and class component in react. I hope this helps :)&lt;/p&gt;

</description>
      <category>react</category>
      <category>component</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
