<?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: Abubaker Saeed</title>
    <description>The latest articles on DEV Community by Abubaker Saeed (@abubakersaeed).</description>
    <link>https://dev.to/abubakersaeed</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%2F399222%2Fabcf6804-8689-41b1-9d9c-93b3eb657036.jpg</url>
      <title>DEV Community: Abubaker Saeed</title>
      <link>https://dev.to/abubakersaeed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abubakersaeed"/>
    <language>en</language>
    <item>
      <title>React Hooks: Understanding the &lt;useState&gt; hook and then building a mini-app with it</title>
      <dc:creator>Abubaker Saeed</dc:creator>
      <pubDate>Mon, 23 Aug 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/abubakersaeed/react-hooks-understanding-the-usestate-hook-and-then-building-a-mini-app-with-it-2pcc</link>
      <guid>https://dev.to/abubakersaeed/react-hooks-understanding-the-usestate-hook-and-then-building-a-mini-app-with-it-2pcc</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This article is originally published on my website and if you'd prefer reading on my website, here's the link &lt;a href="https://www.abubakersaeed.com/blog/react-usestate-hook/"&gt;abubakersaeed.com/blog/react-usestate-hook&lt;/a&gt;&lt;/em&gt; 🙂&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In React v16.8, the team introduced the Hooks API, which pretty much changed the way we write React. In this article, we're going to explore the &lt;code&gt;useState&lt;/code&gt; hook.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; hook is mostly used to set up and control the state of the component. It's just a function that we call and pass the initial value in — which gives us a returned array that includes two items; the first one is &lt;em&gt;current value&lt;/em&gt; and the second one is a &lt;em&gt;function&lt;/em&gt; which we can use to update the current value.&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, {useState} from "react";

...

let [value, setValue] = useState("Forest");

...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; We're using array destructuring to store both items in variables. If it's new to you, you can learn about it at MDN (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment&lt;/a&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitation: Hooks can only be used in functions
&lt;/h2&gt;

&lt;p&gt;If you're React developer for a long time like me, you'd know that React is mostly all about class-based components — with the release of the Hooks API, they've changed that. To use hooks we have to go with a functional approach otherwise Hooks won't be going to work.&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, {useState} from "react";

// &amp;lt;App /&amp;gt; Component
function App() {

  // useState hook
  let [value, setValue] = useState("Forest");

  // JSX
  return (
    &amp;lt;h1&amp;gt;{value}&amp;lt;/h1&amp;gt;
  )

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;&amp;lt;App /&amp;gt;&lt;/code&gt; component is going to render a heading with a text displaying the value on the screen which in our case is "Forest" — which we've passed as our initial value in the &lt;code&gt;useState&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;That's all! This is the syntax and minimal example of the &lt;code&gt;useState&lt;/code&gt; hook. It's a simple and common hook — but in my opinion, &lt;em&gt;extremely&lt;/em&gt; important React hook.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building a mini-app
&lt;/h2&gt;

&lt;p&gt;We'll be going to create a mini-app, where the focus will only be on a &lt;code&gt;useState&lt;/code&gt; hook (~in action).&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, {useState} from "react";

function App() {

  let [appleEaten, setAppleEaten] = useState("Not Yet!");

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;React Mini App&amp;lt;/h1&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;div&amp;gt;Q: Are you done eating Apple?&amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;A: {appleEaten}&amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The items in the returned array from the &lt;code&gt;useState&lt;/code&gt; function, we can name their variables anything — meaning we don't have to give them a name of value and setValue — we can give them any name we want, however, it is a good practice that for the second variable we give it a same name as the first one, uppercase the first letter and add "set" before it. That way it'll be easy for us to remember what the second variable will do e.g [&lt;code&gt;color&lt;/code&gt;, &lt;code&gt;setColor&lt;/code&gt;].&lt;/p&gt;

&lt;p&gt;Now let's update the &lt;code&gt;appleEaten&lt;/code&gt; value using the &lt;code&gt;setAppleEaten&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Anywhere where we're using the &lt;code&gt;appleEaten&lt;/code&gt; variable, those places are going to update automatically to the newest value without us doing any extra work — In other words, React will handle that for us.&lt;/p&gt;

&lt;p&gt;Alright, add an &lt;code&gt;&amp;lt;button /&amp;gt;&lt;/code&gt; with an &lt;code&gt;onClick&lt;/code&gt; event on it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...

  &amp;lt;div&amp;gt;A: {appleEaten}&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;button onClick={function() { setAppleEaten("Yup!") }}&amp;gt;Change answer&amp;lt;/button&amp;gt;

...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in the browser when we click on the &lt;code&gt;&amp;lt;button /&amp;gt;&lt;/code&gt;, it'll execute the function and inside that function, it will execute the &lt;code&gt;setAppleEaten&lt;/code&gt; function which in result will update the &lt;code&gt;appleEaten&lt;/code&gt; value with the value that we've passed in the &lt;code&gt;setAppleEaten&lt;/code&gt; function and then the places where we are using the &lt;code&gt;appleEaten&lt;/code&gt; variable will going to update automatically to the newest value.&lt;/p&gt;

&lt;p&gt;In our app, we've only used the &lt;code&gt;appleEaten&lt;/code&gt; variable once but we can use it as many times as we want and they all will display the newest/current &lt;code&gt;appleEaten&lt;/code&gt; value in the browser.&lt;/p&gt;

&lt;p&gt;Just for curiosity's sake, change:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;A: {appleEaten}&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;A: {appleEaten} {appleEaten}&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then click the &lt;code&gt;&amp;lt;button /&amp;gt;&lt;/code&gt; in the browser. (Aside: It'll also increase the cuteness in the tone even more! 😊🥰)&lt;/p&gt;

&lt;p&gt;To make it more interactive we can add another &lt;code&gt;&amp;lt;button /&amp;gt;&lt;/code&gt; which will change the &lt;code&gt;appleEaten&lt;/code&gt; value back to "Not Yet!" when clicked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...

&amp;lt;button onClick={function() { setAppleEaten("Yup!") }}&amp;gt;Answer "Yup!"&amp;lt;/button&amp;gt;
&amp;lt;button onClick={function() { setAppleEaten("Not Yet!") }}&amp;gt;Answer "Not Yet!"&amp;lt;/button&amp;gt;

...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Phew! We've made our mini-app — This is the final result of our app along with code on &lt;a href="https://codesandbox.io/s/react-usestate-hook-final-1s4t2"&gt;CodeSandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/1s4t2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;We can add as many states as we want, the way of doing that is the same, we use the &lt;code&gt;useState&lt;/code&gt; function and store items from the returned array in variables and so on... Also, we're not limited to only the 'String' value, meaning we can pass anything e.g array, object, boolean, etc. in the &lt;code&gt;useState&lt;/code&gt; function.&lt;/p&gt;

&lt;h4&gt;
  
  
  Further reading
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Using the State Hook (&lt;a href="https://reactjs.org/docs/hooks-state.html"&gt;https://reactjs.org/docs/hooks-state.html&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Rules of Hooks (&lt;a href="https://reactjs.org/docs/hooks-rules.htm"&gt;https://reactjs.org/docs/hooks-rules.htm&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope you've enjoyed the article as much as I've enjoyed writing it and found it helpful!&lt;/p&gt;

&lt;p&gt;🌿 Abubaker's website: &lt;a href="https://abubakersaeed.com"&gt;https://abubakersaeed.com&lt;/a&gt; &lt;br&gt;
🌱 Social: &lt;a href="https://twitter.com/AbubakerSaeed96"&gt;Twitter&lt;/a&gt; | &lt;a href="https://codepen.io/AbubakerSaeed"&gt;CodePen&lt;/a&gt; | &lt;a href="https://github.com/AbubakerSaeed"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
