<?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>Forem: Emal Isuranga</title>
    <description>The latest articles on Forem by Emal Isuranga (@emal_isuranga_22428bdd80e).</description>
    <link>https://forem.com/emal_isuranga_22428bdd80e</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%2F1607597%2F015a61c6-1483-4799-ae29-e3d451ae73d6.jpg</url>
      <title>Forem: Emal Isuranga</title>
      <link>https://forem.com/emal_isuranga_22428bdd80e</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/emal_isuranga_22428bdd80e"/>
    <language>en</language>
    <item>
      <title>Why Do We Write `super(props)` in React?</title>
      <dc:creator>Emal Isuranga</dc:creator>
      <pubDate>Tue, 11 Jun 2024 05:56:13 +0000</pubDate>
      <link>https://forem.com/emal_isuranga_22428bdd80e/why-do-we-write-superprops-in-react-3g08</link>
      <guid>https://forem.com/emal_isuranga_22428bdd80e/why-do-we-write-superprops-in-react-3g08</guid>
      <description>&lt;p&gt;If you've been working with React class components, you've likely come across the &lt;code&gt;super(props)&lt;/code&gt; statement in the constructor method. This might seem a bit confusing, especially for those who are new to JavaScript's class syntax and inheritance model. In this blog post, we'll delve into what &lt;code&gt;super(props)&lt;/code&gt; does and why it's necessary in React class components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding &lt;code&gt;super(props)&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;To understand &lt;strong&gt;&lt;code&gt;super(props)&lt;/code&gt;&lt;/strong&gt;, it's essential to know a bit about JavaScript classes and inheritance. In JavaScript, classes can inherit properties and methods from other classes. This is done using the &lt;strong&gt;extends&lt;/strong&gt; keyword.&lt;/p&gt;

&lt;p&gt;When a class is derived from another class, the derived class inherits the properties and methods of the base class. The &lt;strong&gt;&lt;code&gt;super&lt;/code&gt;&lt;/strong&gt; keyword is used to call the constructor of the base class and initialize the &lt;code&gt;this&lt;/code&gt; context in the derived class.&lt;/p&gt;

&lt;h4&gt;
  
  
  The &lt;code&gt;super&lt;/code&gt; Keyword
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;super&lt;/code&gt; keyword refers to the parent class. When used in a constructor, it must be called before accessing &lt;code&gt;this&lt;/code&gt; in the derived class. Failing to call &lt;code&gt;super&lt;/code&gt; before accessing &lt;code&gt;this&lt;/code&gt; will result in a ReferenceError.&lt;/p&gt;

&lt;h4&gt;
  
  
  The &lt;code&gt;props&lt;/code&gt; Argument
&lt;/h4&gt;

&lt;p&gt;In React, components can accept inputs, called &lt;strong&gt;props&lt;/strong&gt;, which are passed down from parent components. When we define a class component, we often want to access these props within our component.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Constructor in React Components
&lt;/h2&gt;

&lt;p&gt;In React class components, the constructor is used for two main purposes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Initializing state&lt;/strong&gt;: If you need to set the initial state of your component, you can do so in the constructor.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Binding event handlers&lt;/strong&gt;: If you need to bind methods to the class in stance, you can do this in the constructor.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s a basic example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { Component } from 'react';

class MyComponent extends Component {
  constructor(props) {
    super(props);
    // Now 'this' is initialized and we can set the initial state
    this.state = {
      message: 'Hello, world!'
    };
  }

  render() {
    return (
      &amp;lt;div&amp;gt;{this.state.message}&amp;lt;/div&amp;gt;
    );
  }
}

export default MyComponent;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, &lt;code&gt;super(props)&lt;/code&gt; is called inside the constructor. Let's break down why this is necessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Call &lt;code&gt;super(props)&lt;/code&gt;?
&lt;/h2&gt;

&lt;h4&gt;
  
  
  1. Accessing &lt;code&gt;this.props&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;When you call &lt;code&gt;super(props)&lt;/code&gt;, you pass the &lt;code&gt;props&lt;/code&gt; argument to the constructor of the parent class, which is &lt;code&gt;React.Component&lt;/code&gt; in this case. This allows you to access &lt;code&gt;this.props&lt;/code&gt; inside the constructor and throughout the component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyComponent extends Component {
  constructor(props) {
    super(props);
    console.log(this.props); // Props are now accessible
  }

  render() {
    return (
      &amp;lt;div&amp;gt;{this.props.message}&amp;lt;/div&amp;gt;
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without calling &lt;code&gt;super(props)&lt;/code&gt;, &lt;code&gt;this.props&lt;/code&gt; would be undefined inside the constructor, which could lead to unexpected behavior and bugs.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Ensuring Proper Initialization
&lt;/h4&gt;

&lt;p&gt;Calling &lt;code&gt;super(props)&lt;/code&gt; ensures that the base class (React.Component) is properly initialized with the props passed to it. This is necessary for React's internal component management and lifecycle methods to work correctly.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Following ES6 Class Syntax Rules
&lt;/h4&gt;

&lt;p&gt;According to the ES6 class specification, if a derived class has a constructor, it must call &lt;code&gt;super()&lt;/code&gt; before accessing &lt;code&gt;this&lt;/code&gt;. This is a rule enforced by JavaScript to ensure that the base class is properly initialized.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Happens if You Don’t Call &lt;code&gt;super(props)&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;If you forget to call &lt;code&gt;super(props)&lt;/code&gt; in a React class component, you’ll encounter an error when trying to access &lt;code&gt;this.props&lt;/code&gt; or &lt;code&gt;this.state&lt;/code&gt; inside the constructor. 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 MyComponent extends Component {
  constructor(props) {
    // super(props); // Uncommenting this will fix the error
    console.log(this.props); // ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
  }

  render() {
    return (
      &amp;lt;div&amp;gt;{this.props.message}&amp;lt;/div&amp;gt;
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code will throw a &lt;code&gt;ReferenceError&lt;/code&gt; because &lt;code&gt;super(props)&lt;/code&gt; is not called, which means &lt;code&gt;this&lt;/code&gt; is not properly initialized.&lt;/p&gt;

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

&lt;p&gt;In summary, &lt;code&gt;super(props)&lt;/code&gt; in React class components is crucial for the following reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It allows you to access &lt;code&gt;this.props&lt;/code&gt; inside the constructor.&lt;/li&gt;
&lt;li&gt;It ensures that the base class (React.Component) is properly initialized.&lt;/li&gt;
&lt;li&gt;It follows the ES6 class syntax rules, ensuring that &lt;code&gt;this&lt;/code&gt; is correctly initialized before use.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While React has moved towards functional components and hooks for most new development, understanding how class components work is still valuable, especially for maintaining and upgrading older codebases. So, the next time you see &lt;code&gt;super(props)&lt;/code&gt;, you'll know exactly why it's there and what it does.&lt;/p&gt;

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