<?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: Logan Johnston</title>
    <description>The latest articles on DEV Community by Logan Johnston (@johnstonlogan).</description>
    <link>https://dev.to/johnstonlogan</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%2F420142%2Ff95ea847-f42b-4064-b168-81c064997f44.jpg</url>
      <title>DEV Community: Logan Johnston</title>
      <link>https://dev.to/johnstonlogan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/johnstonlogan"/>
    <language>en</language>
    <item>
      <title>useState() vs setState() - Strings, Objects, and Arrays</title>
      <dc:creator>Logan Johnston</dc:creator>
      <pubDate>Tue, 01 Sep 2020 03:46:27 +0000</pubDate>
      <link>https://dev.to/johnstonlogan/react-hooks-barney-style-1hk7</link>
      <guid>https://dev.to/johnstonlogan/react-hooks-barney-style-1hk7</guid>
      <description>&lt;p&gt;The purpose of this article is to break down the use of the useState() React hook in an easy way using strings, objects, and arrays. We will also take a look at how these would be handled in class components. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer&lt;/em&gt; - I would normally create an onChange function separately but I find it easier to understand with an inline function.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the setState function?
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;setState&lt;/code&gt; function is used to handle the state object in a React class component. This is something you will see a lot of in the examples below. Anytime you see a &lt;code&gt;this.setState()&lt;/code&gt; this is how we are setting the state in a class component. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is a hook in React?
&lt;/h3&gt;

&lt;p&gt;React hooks were introduced in React v16.8. They allow you to use state and other React features without the need to create a class. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Examples:&lt;/em&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Class component
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvf4q8iz5bc0x8kylh0jq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvf4q8iz5bc0x8kylh0jq.png" alt="React Class Component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Functional component
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frfu8sdb387ckbnw08lsu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frfu8sdb387ckbnw08lsu.png" alt="React Function Component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While these two code snippets look similar they do have slight differences in syntax, lifecycle methods, and state management.&lt;/p&gt;

&lt;h3&gt;
  
  
  setState() vs useState() - Strings.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;setState()&lt;/strong&gt; Class Component&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using state in a class component requires the building of a state object. This state object is then modified by calling this.setState("new state"). &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fowixbk8ic9uuqbcl97bg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fowixbk8ic9uuqbcl97bg.png" alt="Class-String"&gt;&lt;/a&gt;&lt;br&gt;
In this example, we've created a &lt;code&gt;state = { value: '' }&lt;/code&gt; object which has a &lt;code&gt;value&lt;/code&gt; key and that key is initialized as an empty string. We've assigned an &lt;code&gt;onChange&lt;/code&gt; event to the input so that every time we add or remove a character to the input we are calling the &lt;code&gt;this.setState()&lt;/code&gt;. Here we areupdating the state using the value of the input (&lt;code&gt;e.target.value&lt;/code&gt;) and setting it to the components state.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;useState()&lt;/strong&gt; Functional Component&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With a functional component, we can use React hooks, specifically the &lt;code&gt;useState()&lt;/code&gt; hook. This simplifies the creation of a state component and the function that updates it. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzjem78le3anmxkx57u35.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzjem78le3anmxkx57u35.png" alt="Hook-String"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We import &lt;code&gt;{useState}&lt;/code&gt; from React and we are able to simply create a state and a function to set that state (state: &lt;code&gt;value&lt;/code&gt;, setState: &lt;code&gt;setValue&lt;/code&gt;). The initial state of this component is set when calling &lt;code&gt;useState&lt;/code&gt;, in this example, we are setting it to an empty string (&lt;code&gt;useState("")&lt;/code&gt;). The only difference between the functional component and the class component at this point is instead of calling &lt;code&gt;this.setState&lt;/code&gt; we use the function we created in the &lt;code&gt;useState&lt;/code&gt;, in this case, &lt;code&gt;setValue&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  setState() vs useState() - Objects.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;setState()&lt;/strong&gt; Class Component&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since &lt;code&gt;state&lt;/code&gt; in a class component is already an object, it's business as usual. Use &lt;code&gt;setState&lt;/code&gt; to populate the values of the state object. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzox15pw74vwqe6dg3f7l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzox15pw74vwqe6dg3f7l.png" alt="Class Object"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the example above the users &lt;code&gt;userName&lt;/code&gt; and &lt;code&gt;email&lt;/code&gt; is stored in the state similar to the string version we talked about above.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;useState()&lt;/strong&gt; Functional Component&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When we want to use the &lt;code&gt;useState&lt;/code&gt; hook for an object we are going to initialize it to an empty object &lt;code&gt;useState({})&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fabdoggxxaukzafvtgr26.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fabdoggxxaukzafvtgr26.png" alt="Functional-object"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we are using the same &lt;code&gt;setValue&lt;/code&gt; that we did in the string example but we've added a few things to our &lt;code&gt;setValue&lt;/code&gt; function. First, we use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax" rel="noopener noreferrer"&gt;spread&lt;/a&gt; syntax to expand the current &lt;code&gt;value&lt;/code&gt; before we add a new key-value pair. Second, we dynamically set the key using &lt;code&gt;[e.target.name]&lt;/code&gt;, in this case, we are creating the key using the input's "name" attribute. Lastly, we are setting that key's value to the &lt;code&gt;e.target.value&lt;/code&gt;. So after using the inputs we have an object with two keys &lt;code&gt;{userName: "", email: ""}&lt;/code&gt; and their values. &lt;br&gt;
Creating an object could also be accomplished using multiple &lt;code&gt;useState&lt;/code&gt; hooks and then bundling them into an object later if needed. See the example below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F13fh9dmqx5zu527w8lv8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F13fh9dmqx5zu527w8lv8.png" alt="Functional-Objectv.2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: I have my own preference for how to deal with objects while using hooks, and as you get more familiar you may find you enjoy either the class or functional component more than the other. &lt;/p&gt;

&lt;h3&gt;
  
  
  setState() vs useState() - Arrays.
&lt;/h3&gt;

&lt;p&gt;Using arrays in stateful components can be extremely powerful, especially when creating things like a todo list. In these examples, we will be creating a very simple todo list.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;setState()&lt;/strong&gt; Class Component&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When using an array in a stateful class component we need at least two keys in our state object. One would be the array itself &lt;code&gt;todoArr: []&lt;/code&gt; and the other would be the value that we are going to be pushing into the array &lt;code&gt;todo: ""&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnn3lmcf104k60v63pszn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnn3lmcf104k60v63pszn.png" alt="Class-Array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we use the &lt;code&gt;onChange&lt;/code&gt; attribute for our input to set the &lt;code&gt;todo&lt;/code&gt; in our state object. We then have our &lt;code&gt;Add Item&lt;/code&gt; button which when clicked will call our &lt;code&gt;addItem&lt;/code&gt; function. In the &lt;code&gt;addItem&lt;/code&gt; function we are going to create a list variable which is is an array that spreads the current &lt;code&gt;todoArr&lt;/code&gt; and then adds the new &lt;code&gt;todo&lt;/code&gt; item to the end of it. After creating the list array we use the &lt;code&gt;setState&lt;/code&gt; function to replace the current &lt;code&gt;todoArr&lt;/code&gt; with the new one and then set the &lt;code&gt;todo&lt;/code&gt; back to an empty string to clear the input. Lastly at the bottom, we map through the current &lt;code&gt;todoArr&lt;/code&gt;. The &lt;code&gt;setState&lt;/code&gt; function will cause the component to rerender so every time you add an item it is immediately rendered onto the page.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;useState()&lt;/strong&gt; Functional Component&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dealing with the hooks in a function component seems extremely similar to the class component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fs2s5lj3kt0h3ei3grkhy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fs2s5lj3kt0h3ei3grkhy.png" alt="Function-Array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We use the &lt;code&gt;setTodo&lt;/code&gt; function to set our &lt;code&gt;todo&lt;/code&gt; value in the &lt;code&gt;onChange&lt;/code&gt; attribute of our input. We then have the same &lt;code&gt;addItem&lt;/code&gt; function attached to the click of our Add Item button. The only difference we see here is that we don't create a list variable to pass into the hook. We could have avoided this in the class component but I think the readability when using the variable is much better. With the hook, I don't think the use of creating the &lt;code&gt;list&lt;/code&gt; array beforehand is needed. We can spread the current array, add the new item, and then set the current &lt;code&gt;todo&lt;/code&gt; back to an empty string so we can clear the input.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;While using functional components with hooks is the new hotness, the state management is still very similar to the class components. If you're looking to start using function components with hooks over class components hopefully this post has helped you understand a little bit more about how to implement them. &lt;/p&gt;

</description>
      <category>react</category>
      <category>hook</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
