<?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: Britney Smith</title>
    <description>The latest articles on DEV Community by Britney Smith (@britneys).</description>
    <link>https://dev.to/britneys</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%2F309149%2Fd77ca34c-f57e-4bdc-8062-f14385e05cc2.jpg</url>
      <title>DEV Community: Britney Smith</title>
      <link>https://dev.to/britneys</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/britneys"/>
    <language>en</language>
    <item>
      <title>Creating Reusable React Components with map()</title>
      <dc:creator>Britney Smith</dc:creator>
      <pubDate>Wed, 22 Jan 2020 14:51:49 +0000</pubDate>
      <link>https://dev.to/britneys/creating-reusable-react-components-with-map-4823</link>
      <guid>https://dev.to/britneys/creating-reusable-react-components-with-map-4823</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When creating components in React, we sometimes notice that certain components are repeated. Using Javascript's &lt;code&gt;map()&lt;/code&gt; method, we can make these repetitive components more reusable. In this post, we'll explore a method of creating reusable card components using this gem of &lt;a href="https://en.wikipedia.org/wiki/Functional_programming"&gt;functional programming&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;map()&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;The method &lt;code&gt;map()&lt;/code&gt; is a higher-order Javascript&lt;sup id="fnref1"&gt;1&lt;/sup&gt; method that can be called on an array, and returns another array. The results of the returned array depend on what we tell the method to do to each element of the array. We tell &lt;code&gt;map&lt;/code&gt; what we want to do to each array element by passing a callback. &lt;/p&gt;

&lt;p&gt;Below, I've used the simplest version of this callback: where its only argument is the individual array element, and the work we want done to each of those elements is returned in the callback function. After the &lt;code&gt;map&lt;/code&gt; method iterates through each array element and does the work on that element, it returns an array with the modified elements. The elements in the returned array are in the same order that they were sequenced in the original array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mappedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [2, 3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The callback can accept other arguments, like the index of the array elements being iterated over. Check the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;docs&lt;/a&gt; for more information!&lt;/p&gt;

&lt;h2&gt;
  
  
  The source array
&lt;/h2&gt;

&lt;p&gt;First, we need an array to call the map method on. Here, I'm choosing to make things a bit more interesting by using an array of Javascript objects, each representing a superhero or supervillain. Each object has a property of &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;alterEgo&lt;/code&gt;, and &lt;code&gt;alignment&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;characters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wonder Woman&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;alterEgo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Diana Prince&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;alignment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hero&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Poison Ivy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;alterEgo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pamela Lillian Isley&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;alignment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;villain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Black Canary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;alterEgo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dinah Drake&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;alignment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hero&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Catwoman&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;alterEgo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Selina Kyle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;alignment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;villain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  The component
&lt;/h2&gt;

&lt;p&gt;Next, we'll need the component. We're making an unordered list where a card component represents a list item. This is more semantic and accessible than creating a list with just &lt;code&gt;div&lt;/code&gt;s. Below, I've provided the skeleton for our component, that has placeholders for where the name, alter-ego, and the alignment values will go.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const CardList = () =&amp;gt; {
    return (
        &amp;lt;ul&amp;gt;
            &amp;lt;li&amp;gt;
                &amp;lt;div className="card-container"&amp;gt;
                    &amp;lt;p&amp;gt;
                        &amp;lt;strong&amp;gt;Name&amp;lt;/strong&amp;gt;
                    &amp;lt;/p&amp;gt;
                    &amp;lt;p&amp;gt;AlterEgo&amp;lt;/p&amp;gt;
                    &amp;lt;p&amp;gt;Alignment&amp;lt;/p&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt;
    );
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Putting it together
&lt;/h2&gt;

&lt;p&gt;With &lt;code&gt;map()&lt;/code&gt;, we can return an array of functions. Functions are "first-class citizens" in Javascript, so they can be passed around and returned like any string or number (which is why we can pass callbacks in the first place!). React components themselves are functions: &lt;a href="https://reactjs.org/docs/jsx-in-depth.html"&gt;written in JSX, the &lt;code&gt;React.createElement()&lt;/code&gt; method is being called under the hood&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With our source array and list element above, we can return an array of list elements that are populated with the values of the character object properties we choose to access.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const CardList = () =&amp;gt; {
  return (
    &amp;lt;ul&amp;gt;
      {characters.map(character =&amp;gt; {
        return (
          &amp;lt;li&amp;gt;
            &amp;lt;div className="card-container"&amp;gt;
              &amp;lt;p&amp;gt;
                &amp;lt;strong&amp;gt;{character.name}&amp;lt;/strong&amp;gt;
              &amp;lt;/p&amp;gt;
              &amp;lt;p&amp;gt;{character.alterEgo}&amp;lt;/p&amp;gt;
              &amp;lt;p&amp;gt;{character.alignment}&amp;lt;/p&amp;gt;
            &amp;lt;/div&amp;gt;
          &amp;lt;/li&amp;gt;
        );
      })}
    &amp;lt;/ul&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, we call &lt;code&gt;map()&lt;/code&gt; on our array of objects, and we are supplying a callback that tells &lt;code&gt;map()&lt;/code&gt; what we want done to each object. Each object in the array is the element that is passed as an argument to that callback. As we are iterating, we are returning a list item element. In the paragraph tags, we are accessing the value of each object's property.&lt;/p&gt;

&lt;h2&gt;
  
  
  🌟 Bonus! Refactoring
&lt;/h2&gt;

&lt;p&gt;We can take this component to the next level by making the list item into its own component.&lt;/p&gt;

&lt;p&gt;As a best practice, we should also add a unique &lt;code&gt;key&lt;/code&gt; to each list item. This helps React re-render components more efficiently, since it now only has to observe changes in a particular, uniquely identified repeated element in order to re-render, instead of re-rendering the entire component array when any one component element changes. Again, &lt;a href="https://reactjs.org/docs/lists-and-keys.html#keys"&gt;see the docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For the refactor, we'll want to use the unique &lt;code&gt;id&lt;/code&gt; property of each element in the array for the list item &lt;code&gt;key&lt;/code&gt;. If our source data was coming from some kind of database, using that record's &lt;a href="https://en.wikipedia.org/wiki/Primary_key"&gt;primary key&lt;/a&gt; would work here, too.&lt;/p&gt;

&lt;p&gt;The only thing that our list item component needs is the character object, so we'll pass that in as a prop. I've added some inline styling&lt;sup id="fnref2"&gt;2&lt;/sup&gt; to make the list items look more like cards.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const CardListItem = props =&amp;gt; {
  return (
    &amp;lt;li&amp;gt;
      &amp;lt;div
        className="card-container"
        style={{
          width: "50%",
          border: "solid 3px #d3d3d3",
          margin: "10px auto"
        }}
      &amp;gt;
        &amp;lt;p&amp;gt;
          &amp;lt;strong&amp;gt;{props.character.name}&amp;lt;/strong&amp;gt;
        &amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;{props.character.alterEgo}&amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;{props.character.alignment}&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/li&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And this is how &lt;code&gt;CardListItem&lt;/code&gt; can be used. Notice that we are using the character object's &lt;code&gt;id&lt;/code&gt; property as the component's &lt;code&gt;key&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const CardList = () =&amp;gt; {
  return (
    &amp;lt;ul style={{ listStyleType: "none" }}&amp;gt;
      {characters.map(character =&amp;gt; {
        return &amp;lt;CardListItem character={character} key={character.id} /&amp;gt;;
      })}
    &amp;lt;/ul&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And that's it! &lt;/p&gt;

&lt;p&gt;⚡️&lt;a href="https://codesandbox.io/s/reusable-card-component-n2ifm"&gt;Try it out on Codesandbox!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/reusable-card-component-n2ifm"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;Map can be called on arrays and other data structures in other languages as well, and it works in a similar way! ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/list-style-type#Accessibility_concerns"&gt;See the docs&lt;/a&gt; for info on how to make unordered lists with no list style type more accessible. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>react</category>
      <category>functional</category>
      <category>javascript</category>
      <category>a11y</category>
    </item>
  </channel>
</rss>
