<?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: Cobbygraves</title>
    <description>The latest articles on DEV Community by Cobbygraves (@codescript).</description>
    <link>https://dev.to/codescript</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%2F703369%2F23d73e7b-1a43-4d8c-9922-d0291159cea1.jpg</url>
      <title>DEV Community: Cobbygraves</title>
      <link>https://dev.to/codescript</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codescript"/>
    <language>en</language>
    <item>
      <title>componentWillUnmount lifecycle method</title>
      <dc:creator>Cobbygraves</dc:creator>
      <pubDate>Tue, 22 Mar 2022 03:21:52 +0000</pubDate>
      <link>https://dev.to/codescript/componentwillunmount-lifecycle-method-2geo</link>
      <guid>https://dev.to/codescript/componentwillunmount-lifecycle-method-2geo</guid>
      <description>&lt;p&gt;This is &lt;em&gt;part III&lt;/em&gt; of the tutorial series &lt;strong&gt;&lt;a href="https://dev.to/codescript/class-component-vs-functional-componentlifecycle-methods-1kcl"&gt;Class Component Vs Functional Component(Lifecycle Methods)&lt;/a&gt;&lt;/strong&gt;.  To implement the &lt;strong&gt;componentWillUnmount&lt;/strong&gt; lifecycle method in a class-based component, let consider the code snippet below: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fntbmrs6wgl9ao7jsg332.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fntbmrs6wgl9ao7jsg332.png" alt="componentWillUnmount in class component" width="599" height="299"&gt;&lt;/a&gt;&lt;br&gt;
Likewise, to implement the same code in a functional component let’s consider the code snippet below: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5d0kx97gj3vdcf76unz0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5d0kx97gj3vdcf76unz0.png" alt="componentWillUnmount in functional component" width="556" height="359"&gt;&lt;/a&gt;&lt;br&gt;
Now, analyzing the two code snippet we can conclude that for the class-based component, the &lt;strong&gt;componentWillUnmount&lt;/strong&gt; function is defined in the class.&lt;br&gt;
On the other hand, to implement the same code in a functional component, we have to return an anonymous function inside the &lt;strong&gt;useEffect&lt;/strong&gt; hook. Inside the return function, we write the code for the clean-up.&lt;br&gt;
Concluding this series, these methods are used to run side effects in a component lifecycle and must be used when it is necessary.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>componentDidUpdate lifecyle method</title>
      <dc:creator>Cobbygraves</dc:creator>
      <pubDate>Tue, 22 Mar 2022 03:01:30 +0000</pubDate>
      <link>https://dev.to/codescript/componentdidupdate-lifecyle-method-o5k</link>
      <guid>https://dev.to/codescript/componentdidupdate-lifecyle-method-o5k</guid>
      <description>&lt;p&gt;This is &lt;em&gt;part II&lt;/em&gt; of my tutorial series &lt;strong&gt;&lt;a href="https://dev.to/codescript/class-component-vs-functional-componentlifecycle-methods-1kcl"&gt;Class Component Vs Functional Component(Lifecycle Methods).&lt;/a&gt;&lt;/strong&gt; If you haven’t already read &lt;em&gt;part I&lt;/em&gt;, you can do so to get a better understanding of this tutorial series going forward.&lt;br&gt;
To implement &lt;strong&gt;componentDidUpdate&lt;/strong&gt; in a class-based component let’s consider the code snippet below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi3ankkehrddum8j11fpt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi3ankkehrddum8j11fpt.png" alt="componentDidUpdate in class component" width="608" height="317"&gt;&lt;/a&gt;&lt;br&gt;
To implement the same code in a functional component let consider the code snippet below: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy9pf2qi48ta8apt24a4p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy9pf2qi48ta8apt24a4p.png" alt="componentDidUpdate in functional component" width="597" height="228"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s discuss the two code snippet above and see how the &lt;strong&gt;componentDidUpdate&lt;/strong&gt; lifecycle method is implemented in each of them. &lt;br&gt;
Looking at the class-based component, the &lt;strong&gt;componentDidUpdate&lt;/strong&gt; method was defined inside the class component as the way of implementation.&lt;br&gt;
On the other hand, if we look at the functional-based component, the &lt;strong&gt;useEffect&lt;/strong&gt; hook was used to implement the &lt;strong&gt;componentDidUpdate&lt;/strong&gt;. &lt;br&gt;
&lt;strong&gt;NB:&lt;/strong&gt; The &lt;strong&gt;useEffect&lt;/strong&gt; hook takes an optional array of dependencies. Whenever the &lt;strong&gt;useEffect&lt;/strong&gt; hook is configured to have an array of dependencies, it is equivalent to the &lt;strong&gt;componentDidUpdate&lt;/strong&gt; function in the class-based component.&lt;br&gt;
Therefore anytime any of the values in the dependency array changes, the component has to re-render to update the &lt;strong&gt;DOM&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Demystifying Props in React Components</title>
      <dc:creator>Cobbygraves</dc:creator>
      <pubDate>Fri, 28 Jan 2022 02:56:00 +0000</pubDate>
      <link>https://dev.to/codescript/demystifying-props-in-react-components-17ei</link>
      <guid>https://dev.to/codescript/demystifying-props-in-react-components-17ei</guid>
      <description>&lt;p&gt;This tutorial aims at two things about &lt;strong&gt;props&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The purpose of &lt;strong&gt;props&lt;/strong&gt; in react component&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How to use &lt;strong&gt;props&lt;/strong&gt; in react component&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First of all let understand the purpose of &lt;strong&gt;props&lt;/strong&gt; in react components. Basically, &lt;strong&gt;props&lt;/strong&gt; is a way of passing data from parent component to child component in a react application.&lt;/p&gt;

&lt;p&gt;I believe that's simple and easy to understand and achieve since react is a one-way data flow library. ie. data is passed from parent to child.&lt;/p&gt;

&lt;p&gt;Now, how do we pass data from child to parent in a react application if the need arises. The answer to the question above is simple, &lt;strong&gt;props&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To better understand my answer, let look at a scenario using the code below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy801xt7g1pcvgo9050cd.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy801xt7g1pcvgo9050cd.PNG" alt="child component without click" width="612" height="471"&gt;&lt;/a&gt;&lt;em&gt;child component&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd370yzf6t2f7rq0qqf7h.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd370yzf6t2f7rq0qqf7h.PNG" alt="parent component without state" width="426" height="347"&gt;&lt;/a&gt;&lt;em&gt;parent component&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now if we consider the two component above, how do we then show the age of the child component inside the parent component ?.&lt;br&gt;
This is what I mean by my previous question ie. &lt;strong&gt;How do we pass data from the child component to the parent component ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The answer still remains as before ie. &lt;strong&gt;props&lt;/strong&gt;. Let see how to accomplish that in our previous code examples.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuh3j1x5scdogq3z4xewz.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuh3j1x5scdogq3z4xewz.PNG" alt="child component with onclick" width="701" height="503"&gt;&lt;/a&gt;&lt;em&gt;child component&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr1a9rhb5qamymmyc91ce.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr1a9rhb5qamymmyc91ce.PNG" alt="parent component with state" width="555" height="385"&gt;&lt;/a&gt;&lt;em&gt;parent component&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Comparing the parent and child component, we can conclude that, to pass the age value from the child component to the parent component, react makes use of what I termed as &lt;strong&gt;"functional &lt;br&gt;
 props"&lt;/strong&gt;. This simply means a &lt;strong&gt;prop&lt;/strong&gt; which is a function.&lt;/p&gt;

&lt;p&gt;The age from the child component must be passed as an argument to the functional prop inside of the child component. This is what is happening in the child component with respect to the &lt;strong&gt;showAge&lt;/strong&gt; functional &lt;strong&gt;prop&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As the age which is a state in the child component is passed as an argument to the &lt;strong&gt;showAge&lt;/strong&gt; prop, it is then received inside the parent component through a function and then displayed in it &lt;strong&gt;JSX&lt;/strong&gt; below.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I hope you learn a lot about props and how data is passed from parent to child and vice versa in react component&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Class Component Vs Functional Component(Lifecycle Methods)</title>
      <dc:creator>Cobbygraves</dc:creator>
      <pubDate>Fri, 28 Jan 2022 02:20:28 +0000</pubDate>
      <link>https://dev.to/codescript/class-component-vs-functional-componentlifecycle-methods-1kcl</link>
      <guid>https://dev.to/codescript/class-component-vs-functional-componentlifecycle-methods-1kcl</guid>
      <description>&lt;p&gt;&lt;em&gt;Before we begin I want to say that this is a three part series tutorial&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 1 - &lt;a href="https://dev.to/codescript/class-component-vs-functional-componentlifecycle-methods-1kcl"&gt;ComponentDidMount&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Part 2 - &lt;a href="https://dev.to/codescript/componentdidupdate-lifecyle-method-o5k"&gt;ComponentDidUpdate&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;part 3 - &lt;a href="https://dev.to/codescript/componentwillunmount-lifecycle-method-2geo"&gt;ComponentWillUnmount&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let me begin by saying that prior to react 16.8 class based component were the defector standard to implement the lifecycle methods in a react component.&lt;br&gt;
The lifecycle methods were as follows;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;componentDidMount&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;componentDidUpdate&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;componentWillUmount&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;componentDidMount&lt;/strong&gt; method is called when the component is mounted to the &lt;strong&gt;DOM&lt;/strong&gt;. The &lt;strong&gt;componentDidUpdate&lt;/strong&gt; method is called anytime the component receives an update to any of it props or state. This update causes the component to re-render to display it current state of the UI. The &lt;strong&gt;componentWillUnmount&lt;/strong&gt; is called when the react component is about to be removed from the DOM&lt;/p&gt;

&lt;p&gt;React hooks were introduced in functional components which made it possible to implement the lifecycle methods in a react component using the &lt;strong&gt;useEffect&lt;/strong&gt; hook.&lt;/p&gt;

&lt;p&gt;Now, let look at how to implement the lifecycle methods in both functional and class based component. &lt;br&gt;
Let consider the class based component and functional component respectively.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4grujbyri37ygwk39boc.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4grujbyri37ygwk39boc.PNG" alt="class based component" width="477" height="233"&gt;&lt;/a&gt;&lt;em&gt;class based component&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs7hlupglv4oi1ojqfceq.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs7hlupglv4oi1ojqfceq.PNG" alt="function based component" width="368" height="201"&gt;&lt;/a&gt;&lt;em&gt;functional based component&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To implement &lt;strong&gt;componentDidMount&lt;/strong&gt; in a class based component let consider the code snippet below: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsuzqk6ji83fie0oueccw.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsuzqk6ji83fie0oueccw.PNG" alt="componentDidMount" width="434" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To implement the same code in a functional component let consider the code snippet below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqey79yizhujvuyl34ph.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqey79yizhujvuyl34ph.PNG" alt="componentDidMount" width="462" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let discuss the two code snippet above and see how the &lt;strong&gt;componentDidMount&lt;/strong&gt; lifecycle method was implemented in each of them.&lt;/p&gt;

&lt;p&gt;If we look at the class based component, the &lt;strong&gt;componentDidMount&lt;/strong&gt; method was just defined inside the class based component to implement it. On the other hand if we look at the function based component the &lt;strong&gt;useEffect&lt;/strong&gt; hook was used to implement the &lt;strong&gt;componentDidMount&lt;/strong&gt;. Remember that the &lt;strong&gt;useEffect&lt;/strong&gt; hook takes an optional array of dependencies. In the functional component the array of dependencies was left empty.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;useEffect&lt;/strong&gt; hook can be configured to have different effect in a functional component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NB:&lt;/strong&gt; Whenever the &lt;strong&gt;useEffect&lt;/strong&gt; hook is configured to have an empty array of dependencies, it is equivalent to the &lt;strong&gt;componentDidMount&lt;/strong&gt; function in a class based component.&lt;/p&gt;

&lt;p&gt;I hope you found value in this tutorial on the first part of the series of the lifecycle methods in react components. Please don't forget to read the other two part of the tutorial for a better understanding of this concept.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
