<?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: Alexander Nnakwue</title>
    <description>The latest articles on DEV Community by Alexander Nnakwue (@firebase007).</description>
    <link>https://dev.to/firebase007</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%2F52666%2Fa1c84972-9043-4ea3-b3f6-0ce660804bfa.jpeg</url>
      <title>DEV Community: Alexander Nnakwue</title>
      <link>https://dev.to/firebase007</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/firebase007"/>
    <language>en</language>
    <item>
      <title>Understanding the new Set object in JavaScript</title>
      <dc:creator>Alexander Nnakwue</dc:creator>
      <pubDate>Thu, 28 Feb 2019 02:27:49 +0000</pubDate>
      <link>https://dev.to/firebase007/demystifying-the-set-object-3k9p</link>
      <guid>https://dev.to/firebase007/demystifying-the-set-object-3k9p</guid>
      <description>&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fit11xbkj5fnwbqnnbbb4.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fit11xbkj5fnwbqnnbbb4.png" alt="Set object"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;new Set&lt;/em&gt; object from the ES2015 latest browser specification has a variety of use cases. In this tutorial, we will have a look at a simple scenario where we explore ways in which this feature can come in handy. &lt;/p&gt;

&lt;p&gt;Note: Before we continue, it should be noted that the &lt;em&gt;new Set&lt;/em&gt; object currently has good browser support and therefore can be used in production applications. If you want to have a look at the browser support or compatibility, you can take a look at it &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#Browser_compatibility" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;According to the &lt;a href="https://developer.mozilla.org/en-US/" rel="noopener noreferrer"&gt;MDN&lt;/a&gt; documentation, the Set object allows you store unique values of any type, whether they are primitive data types like strings, booleans, symbols, null, etc or even objects.&lt;/p&gt;

&lt;p&gt;Below is an example of the syntax of the new Set object, &lt;/p&gt;

&lt;p&gt;&lt;code&gt;new Set([iterable])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this example, the parameter is iterable - which is more like an object or a collection that we can loop through. A simple example of an iterable is an array. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When values are passed to the Set object, they always remain unique and a new Set object is always returned. On the other hand, if nothing is passed to the Set object or if its value is a primitive like null for example, its return value will be empty.&lt;/p&gt;

&lt;p&gt;Moving on, just like every object has a constructor function, where they derive their traits from including methods and properties, all instances of the Set object inherit from the Set prototype. Therefore, the &lt;em&gt;Set.prototype.constructor&lt;/em&gt; is the function that gets returned when an instance prototype is created. Remember when you use the "new" keyword to declare an instance of something? Great! &lt;/p&gt;

&lt;p&gt;Now looking at some of the properties and methods available on the Set prototype, we have the &lt;em&gt;Set.prototype.size&lt;/em&gt;, which returns the number of values in the Set object. Additionally, we also have the &lt;em&gt;Set.prototype.add()&lt;/em&gt; method which as the name implies, adds a new element with the given value to the Set object and returns the new Set object.&lt;/p&gt;

&lt;p&gt;Now lets look at a simple example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myNewSet = new Set();

myNewSet.add(1) 

// returns Set [1]

myNewSet.add(5); 

// returns Set [ 1, 5 ]

myNewSet.add(5); 

// returns Set [ 1, 5 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we have declared a variable &lt;em&gt;myNewSet&lt;/em&gt; which stores a new instance of the Set object in memory. We then used the &lt;em&gt;add&lt;/em&gt; method to populate the variable. We will notice that the last returned Set object has just a single value of "5" instead of two as expected. Well, this exactly validates our initial point that the values in a Set object may only occur once since it is unique in the Set's collection.&lt;/p&gt;

&lt;p&gt;Note: Earlier we mentioned that to check the number of values in the Set object, we should make use of the &lt;em&gt;Set.prototype.size&lt;/em&gt; property. The &lt;em&gt;Set.prototype.length&lt;/em&gt; property does not check the number of values in a Set object as expected but instead returns 0 which is the value of the length property in this case.  &lt;/p&gt;

&lt;p&gt;Another example of applying the new Set object is in the case of removing array duplicates. let's take a look at an example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]

 console.log([...new Set(numbers)]) 

// [2, 3, 4, 5, 6, 7, 32]

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

&lt;/div&gt;



&lt;p&gt;We can see here that the duplicates were flushed out of the array and a new array was returned with unique elements only. Before now, we had to loop through the array and do a bunch of gymnastics to remove the duplicates. With the new Set method, we saw how we intuitively and with lesser lines of code achieved our aim. &lt;/p&gt;

&lt;p&gt;Now to the more interesting part before we conclude, I was going through a short excerpt on Graphql, where I came across another pretty nifty and important use case of the Set object. Let's look at the example because I believe examples help communicate intent more,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var distances = [
  { from: "Tahoe City", to: "Nevada City", distance: 65 },
  { from: "Nevada City", to: "Redwood Valley", distance: 151 },
  { from: "Redwood Valley", to: "Willits", distance: 16 },
  { from: "Willits", to: "Garberville", distance: 68 },
  { from: "Garberville", to: "Shelter Cove", distance: 24 },
  { from: "Garberville", to: "Mendocino", distance: 76 },
  { from: "Mendocino", to: "Redwood Valley", distance: 51 }
];

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

&lt;/div&gt;



&lt;p&gt;Now the case study here is for us to look for a way to remove duplicate cities from the array of objects because we intended to query for the city data and we didn't want duplicates. &lt;/p&gt;

&lt;p&gt;Now the implementation;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; var cities = new Set();

      distances.forEach(d =&amp;gt; {
        cities.add(d.from);
        cities.add(d.to);
      });

      return Array.from(cities);

// returns (7) ["Tahoe City", "Nevada City", "Redwood Valley", "Willits", "Garberville", "Shelter Cove", "Mendocino"]

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

&lt;/div&gt;



&lt;p&gt;Here, first of all, we declared a new instance of the Set object which allows us to inherit from the prototype of the object - its methods and properties. We then looped through the array of objects stored in the distance variable, and for each iteration, we appended the cities to the Set object while removing duplicates in the process.&lt;/p&gt;

&lt;p&gt;We can now see a simple and very useful example where the Set object comes in handy. Also, note the use of the &lt;em&gt;add&lt;/em&gt; method? Great! Now, also notice the Array.from() method? This method creates an array from an iterable object, where the parameter must be an iterable object, so as to enable us to convert it to an array. &lt;/p&gt;

&lt;p&gt;For more information on this method and its use cases, you can check the documentation here at &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, for more information on everything good about the Set Object and for those who want to explore further, you can check the reference documentation on &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#Specifications" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Big thanks to Alex Banks and Eve Porcello and the MoonHighway team for their article, where I extracted the last example from, so as to illustrate and drive home a very vital use case. For more information, you can check them out &lt;a href="https://moonhighway.com/" rel="noopener noreferrer"&gt;https://moonhighway.com/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Resources cited here are from the Mozilla developer network documentation. &lt;/p&gt;

&lt;p&gt;Thanks for taking the time out to read this. I would really appreciate any questions, comments, and general feedback. Keep learning!&lt;/p&gt;

</description>
      <category>es6</category>
      <category>javascript</category>
      <category>set</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Handling Form Inputs in a controlled manner</title>
      <dc:creator>Alexander Nnakwue</dc:creator>
      <pubDate>Wed, 04 Jul 2018 22:53:55 +0000</pubDate>
      <link>https://dev.to/firebase007/controlled-form-inputs-in-react-586c</link>
      <guid>https://dev.to/firebase007/controlled-form-inputs-in-react-586c</guid>
      <description>&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxs6nywaxcpy35c8frvh3.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxs6nywaxcpy35c8frvh3.png" alt="A sample registration form"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.reactjs.org" rel="noopener noreferrer"&gt;&lt;strong&gt;React&lt;/strong&gt;&lt;/a&gt; advocates for a single source of truth when it comes to handling state or data changes. In &lt;strong&gt;HTML&lt;/strong&gt;, form elements like &lt;code&gt;__input__&lt;/code&gt; and &lt;code&gt;__textarea__&lt;/code&gt; maintain and update their &lt;strong&gt;state&lt;/strong&gt; based on user input in an uncontrolled fashion. However, since React provides &lt;strong&gt;a single state store&lt;/strong&gt; for dealing with state updates (via the setState() API), it is best we look at a better way of handling form state in a controlled way - so as to ensure consistency in dealing with state updates all in one place. Of course, the benefits of this as a developer is that it will give you total control over your form input state and allow you to quickly know where a bug is originating from.  &lt;/p&gt;

&lt;p&gt;In this tutorial, we will learn how to handle and keep track of the values entered when a user types into a form &lt;strong&gt;input&lt;/strong&gt; or &lt;strong&gt;textarea&lt;/strong&gt; field. So here, will be looking at a simple case of handling state updates in React. &lt;/p&gt;

&lt;p&gt;Note that, a React state is just a &lt;strong&gt;simple Javascript object&lt;/strong&gt; - which we declare like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  state = {};

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

&lt;/div&gt;



&lt;p&gt;Also, it is advisable not to tamper or mutate this state object directly, but only via the &lt;strong&gt;setState&lt;/strong&gt; method [which we will be looking at later on]. &lt;/p&gt;

&lt;p&gt;Aside: The React team at Facebook has provided a boilerplate for quickly building and bootstrapping React applications. &lt;strong&gt;Create-react-app&lt;/strong&gt; is a tool that helps to quickly bootstrap React applications, so as to get up and running in little time and avoid the complexities involved in dealing with the entire initial setup process. For more on this, please consult the documentation on, &lt;a href="https://facebook.github.io/create-react-app/" rel="noopener noreferrer"&gt;Create React App&lt;/a&gt;. Additionally, for quick prototyping and demos, React also works with just an HTML file and some script tags, without the need for a large setup or installation steps. For more information on this, please do well to check how to &lt;a href="https://reactjs.org/docs/add-react-to-a-website.html" rel="noopener noreferrer"&gt;add a React component to an HTML page&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Now to begin, we create a &lt;strong&gt;stateful&lt;/strong&gt; React Component - this is necessary because we intend to handle state updates for our form input field. To write a stateful component, we use the eS6 class syntax outlined below;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class App extends React.Component {
constructor(...args) {
  super();
  this.state = {
    value: '', 
  }
  this.onHandleChange = this.onHandleChange.bind(this);
}

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

&lt;/div&gt;



&lt;p&gt;Discussion: Here, our App class is extending or inheriting the capabilities of the React component API, which allows us to use declare a constructor function where we can then declare our initial state object. ES6 improves &lt;strong&gt;&lt;em&gt;class inheritance&lt;/em&gt;&lt;/strong&gt; with &lt;strong&gt;extends&lt;/strong&gt; keyword. &lt;strong&gt;super&lt;/strong&gt; lets us refer to the parent object constructor/class, in this case, the React component. This then allows us to set our component state and bind our component methods to the component class. The state object contains a value property which is initially set to an empty string. &lt;/p&gt;

&lt;p&gt;Next, we implement our component method;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onHandleChange (e) {
  console.log(e.target.value); //logs the user event to the console
  this.setState({ value: e.target.value
  })
}

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

&lt;/div&gt;



&lt;p&gt;Note that, we can also declare component methods using the new es6 fat arrow functions, and avoid the need of binding our class methods to the constructor function. This is because arrow functions have their own &lt;em&gt;this&lt;/em&gt; context. To avoid us from delving too much, you can check out how to use arrow functions &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Discussion:- Here, the &lt;em&gt;onHandleChange&lt;/em&gt; component method provides a way for us to be able to update the state value of our input field using the &lt;em&gt;setState&lt;/em&gt; method we mentioned earlier. We can also see that we are updating the state of our value based on the user event on the form input field. Additionally, and like we previously mentioned, the &lt;strong&gt;setState&lt;/strong&gt; method is the only advisable way for dealing with state updates in a React component, instead of directly mutating or changing &lt;code&gt;this.state.value&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, we call the render method, which returns the JSX to be rendered in the browser.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render (){
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;A Simple Form Input Example&amp;lt;/h1&amp;gt;
      &amp;lt;input type = 'text'  onChange = {this.onHandleChange} value = {this.state.value}/&amp;gt;   
    &amp;lt;/div&amp;gt;
  );
}
}

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

&lt;/div&gt;



&lt;p&gt;Discussion: Here, the input field contains a &lt;em&gt;type&lt;/em&gt; of &lt;em&gt;text&lt;/em&gt;. An &lt;em&gt;onChange&lt;/em&gt; attribute which helps to &lt;strong&gt;bind&lt;/strong&gt; the component method to the input field so that update to the state is carried out; and then finally a &lt;em&gt;value&lt;/em&gt; attribute that makes the input field a React specific state-controlled field.&lt;/p&gt;

&lt;p&gt;Finally, we render our app class component to the DOM. We do so by calling the &lt;strong&gt;ReactDOM.render&lt;/strong&gt; method with the component class and the id of the target element in our HTML file like so;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const rootElement = document.getElementById("root");
ReactDOM.render(&amp;lt;App /&amp;gt;, rootElement);

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

&lt;/div&gt;



&lt;p&gt;If you are interested, you can check the link to this article &lt;a href="https://codesandbox.io/s/7m53wv27p6" rel="noopener noreferrer"&gt;here&lt;/a&gt; to get a feel of how it works. Please, don't hesitate to drop your likes, questions, and comments. &lt;/p&gt;

&lt;p&gt;Thanks for reading. Until next time. :)&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>es6</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
