<?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: umar-daraz</title>
    <description>The latest articles on Forem by umar-daraz (@umardaraz).</description>
    <link>https://forem.com/umardaraz</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%2F30576%2Fd7223272-2420-4c09-ac53-49f21feb20a0.jpeg</url>
      <title>Forem: umar-daraz</title>
      <link>https://forem.com/umardaraz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/umardaraz"/>
    <language>en</language>
    <item>
      <title>My Mental model of React Props And State</title>
      <dc:creator>umar-daraz</dc:creator>
      <pubDate>Wed, 22 May 2019 15:47:04 +0000</pubDate>
      <link>https://forem.com/umardaraz/my-mental-model-of-react-props-and-state-408</link>
      <guid>https://forem.com/umardaraz/my-mental-model-of-react-props-and-state-408</guid>
      <description>&lt;p&gt;In this short article I want to present how I think of React State And Props to you.&lt;br&gt;
It is very simplist model, And there is alot more to Props and State then this. But I find it useful to wrap my head around React two fundametal concepts by using this model.&lt;br&gt;
May be you find is useful like me.&lt;/p&gt;
&lt;h2&gt;
  
  
  Props
&lt;/h2&gt;

&lt;p&gt;Props are like parameters of a function. We use these to make our component dynamic like we use parameters to make our function dynamic.&lt;/p&gt;

&lt;p&gt;lets look at a component to display Greetings to Harry Poter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const GreetingHarryPoter = () =&amp;gt; (
    &amp;lt;span&amp;gt;Hello Harry Poter&amp;lt;/span&amp;gt;
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now I want to say greeting to any person not just Harry Potter, I can refactor my component to take in a prop i.e the name of person. And rename my component to more generic name because now I can use this component to say greeting to anybody.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Greeting = ({name}) =&amp;gt; (
    &amp;lt;span&amp;gt;Hello {name}&amp;lt;/span&amp;gt;
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And Now I can say greeting to different persons like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; (
    &amp;lt;Greeting name="Harry Potter"&amp;gt;
    &amp;lt;Greeting name="Hermione Granger"&amp;gt;
    &amp;lt;Greeting name="Ron Weasley"&amp;gt;
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  State
&lt;/h2&gt;

&lt;p&gt;Anything that happen in React App we capture this by changing State.&lt;br&gt;
Like&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User Clicks A button we do change state.&lt;/li&gt;
&lt;li&gt;Network returns data we do change state.&lt;/li&gt;
&lt;li&gt;Anything else happen we change state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets consider a simple example of counter that we can increment or decrement.&lt;/p&gt;

&lt;p&gt;Lets create a component without state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Counter = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button&amp;gt;-&amp;lt;/button&amp;gt;1&amp;lt;button&amp;gt;+&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's introduce state to it.&lt;/p&gt;

&lt;p&gt;We want to set the initial counter value to 1.&lt;br&gt;
But as you click on increment or decrement we want to change the counter value.&lt;br&gt;
To make counter dynamic we initial it with useState hook.&lt;br&gt;
(useState is provided by react to introduce state into our components)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Counter = () =&amp;gt; {
  const [count, setCount] = useState(1);
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button&amp;gt;-&amp;lt;/button&amp;gt;
      {count}
      &amp;lt;button&amp;gt;+&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;we call useState with initial value (i.e 1) and it returns us the value(count) and a function (setCount) to change the value in future.&lt;/p&gt;

&lt;p&gt;Now we have a function that we can call whenever you click on increment(-) or decrement(+) buttons to change value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Counter = () =&amp;gt; {
  const [count, setCount] = useState(1);
  const increment = () =&amp;gt; setCount(count + 1);
  const decrement = () =&amp;gt; setCount(count - 1);
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button onClick={decrement}&amp;gt;-&amp;lt;/button&amp;gt;
      {count}
      &amp;lt;button onClick={increment}&amp;gt;+&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Thats it, now we have a dynamic counter that responds to user clicks by changing state and React take care of rendering the updated state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;At the simplest level we can consider props as parameters to a function.&lt;br&gt;
And we can consider state as React way to update User Interface.&lt;/p&gt;

</description>
      <category>react</category>
      <category>state</category>
      <category>props</category>
    </item>
  </channel>
</rss>
