{"id":2719,"date":"2018-11-23T12:00:47","date_gmt":"2018-11-23T12:00:47","guid":{"rendered":"https:\/\/tinjurewp.com\/jsblog\/?p=2719"},"modified":"2018-11-23T12:00:47","modified_gmt":"2018-11-23T12:00:47","slug":"creating-list-component-in-reactjs","status":"publish","type":"post","link":"https:\/\/learncode.tinjurewp.com\/creating-list-component-in-reactjs\/","title":{"rendered":"Creating List Component in ReactJs"},"content":{"rendered":"<p class=\"note icon-note\">Note: This is part 3 of three-part prerequisite post series. A\u00a0 prior knowledge of these topics is essential before deep diving into <em>Creating List React component<\/em>.<\/p>\n<p>In the previous learning-note post\u00a0<a href=\"https:\/\/tinjurewp.com\/jsblog\/learning-to-work-with-react-components\/\" target=\"_blank\" rel=\"noopener\">Learning to Work With React Components<\/a>, how to build a simple react component using run time <code>&lt;script&gt;<\/code>in a HTML file were discussed. Although slow, this approach is not ideal for react app development, however it is ideal for my current learning purposes.<\/p>\n<p>The purpose of this learning-note post is understand how to build a simple list react component and apply that knowledge to build <code>BlogPost<\/code> component to list posts in a page.<\/p>\n<h5>JavaScript Prerequisite<\/h5>\n<p>Because creating List component <a href=\"https:\/\/reactjs.org\/\" target=\"_blank\" rel=\"noopener\">ReactJS<\/a> is advanced topic, it requires working JS knowledge in the following areas:<\/p>\n<div class=\"article-series\"><strong>Post Series<\/strong><br \/>\nPart 1: <a href=\"https:\/\/tinjurewp.com\/jsblog\/working-with-functions-and-array-map-method-in-reactjs\/\" target=\"_blank\" rel=\"noopener\">JavaScript Fundamentals &#8211; Array.Map(), Arrow Functions &amp; Functions<\/a><br \/>\nPart 2: <a href=\"https:\/\/tinjurewp.com\/jsblog\/understanding-how-props-are-passed-to-react-component\/\" target=\"_blank\" rel=\"noopener\">How to Pass Props (Data Objects) to Component in ReactJS<\/a><br \/>\n<strong>Part 3<\/strong>: Creating List React Component (<strong>this post<\/strong>)<\/div>\n<p>In Part 2, what are props and how props objects are used to pass data from from one component to other is discussed. In this part 3 of three part series, ES6 <code>array.map()<\/code> method is used to transform array of items into list of React element is discussed.<\/p>\n<p>The purpose of this learning-note post is understand how to build a simple list react component and apply that knowledge to build <code>BlogPost<\/code> component to list posts in a page.<\/p>\n<h6><strong>Iterations of Items &#8211; The Basic<br \/>\n<\/strong><\/h6>\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Iterators_and_Generators#Iterators\" target=\"_blank\" rel=\"noopener\">Iteration of items<\/a> is a very basic process and JS provides simple <a title=\"The for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement (usually a block statement) to be executed in the loop.\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/for\"><code>for<\/code><\/a> loops to <a title=\"The map() method creates a new array with the results of calling a provided function on every element in the calling\u00a0array.\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/map\"><code>map()<\/code><\/a> and <a title=\"The filter() method creates a new array with all elements that pass the test implemented by the provided function.\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/filter\"><code>filter()<\/code><\/a> to iterate collection of items (eg. array). In previous posts: <a href=\"https:\/\/tinjurewp.com\/jsblog\/understanding-javascript-arrays\/\" target=\"_blank\" rel=\"noopener\">Understanding JavaScript Arrays<\/a> &amp; <a href=\"https:\/\/tinjurewp.com\/jsblog\/learning-javascript-array-methods\/\" target=\"_blank\" rel=\"noopener\">Learning JavaScript Array Methods<\/a> are discussed in detail. In <a href=\"https:\/\/tinjurewp.com\/jsblog\/working-with-functions-and-array-map-method-in-reactjs\/\" target=\"_blank\" rel=\"noopener\">Part 1<\/a> of this series how <code>Array.Map()<\/code> method is used in React to iterate items in an array is discussed. In the following section, how list of items in array are transform into list of <a href=\"https:\/\/reactjs.org\/docs\/rendering-elements.html\" target=\"_blank\" rel=\"noopener\">React element<\/a> is discussed.<\/p>\n<h4>List &amp; Keys in ReactJs<\/h4>\n<p><a href=\"https:\/\/reactjs.org\/docs\/getting-started.html\" target=\"_blank\" rel=\"noopener\">React Guide<\/a> documentation provides an excellent overview of how <a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html\" target=\"_blank\" rel=\"noopener\">lists &amp; keys<\/a> are used by react to transform array objects into lists of <a href=\"https:\/\/reactjs.org\/docs\/rendering-elements.html\" target=\"_blank\" rel=\"noopener\">elements<\/a>. Examples used in this section are adopted from the <a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html\" target=\"_blank\" rel=\"noopener\">Lists and Keys<\/a> section of the react Guide.<\/p>\n<h6>1. Transforming JS Arrays into React Elements<\/h6>\n<p>Using the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Map#Relation_with_Array_objects\" target=\"_blank\" rel=\"noopener\">JS <code>map()<\/code><\/a> method discussed in the <a href=\"https:\/\/tinjurewp.com\/jsblog\/working-with-functions-and-array-map-method-in-reactjs\/\" target=\"_blank\" rel=\"noopener\">previous post<\/a>, an array of numerical values are listed.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/* JS syntax - numerical*\/\nconst numbers = [10, 20, 30, 40, 50];\nconst halves = numbers.map((number) =&gt; number \/ 2);\nconsole.log(halves);\/\/ 5, 10, 15, 20, 25<\/code><\/pre>\n<p>In the example above, JS <code>map()<\/code> function is used to list an array of <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Number&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;numbers&lt;\/a&gt;<\/code> (line 2) and their values are divided into <em>halves<\/em> (line 3). The returned value from the <code>map()<\/code> function, which is an new array, is assigned to a variable <code>halves<\/code> (line 3). The <code>console.log<\/code> result shows <code>[5, 10, 15, 20, 25]<\/code> (line4).<\/p>\n<p>The <code>map()<\/code> function can also be applied to an array of <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;string&lt;\/a&gt;<\/code> items as shown below:<\/p>\n<pre class=\"line-numbers\" data-start=\"5\"><code class=\"language-javascript\">\/* JS syntax   - strings*\/\nconst items = [&#039;React&#039;, &#039;JavaScript&#039;, &#039;WordPress&#039;, &#039;Gutenberg&#039;];\nconst newItems = items.map((item) =&gt; item);\nconsole.log(newItems);\n\/\/OUTPUT\n[&quot;React&quot;, &quot;JavaScript&quot;, &quot;WordPress&quot;, &quot;Gutenberg&quot;]<\/code><\/pre>\n<p>The <code>console.log<\/code> output above (line 10) shows returned value of <code>newItems<\/code> array (line 7), which is a new array returned by <code>map()<\/code> function on the original <code>items<\/code> array (line 6).<\/p>\n<p><strong>Transforming an array into list of\u00a0 <code>&lt;a href=&quot;https:\/\/reactjs.org\/docs\/rendering-elements.html&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;elements&lt;\/a&gt;<\/code><\/strong> is achieved by <a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html#rendering-multiple-components\" target=\"_blank\" rel=\"noopener\">including them in JSX using curly braces <code>{ }<\/code><\/a>. In the example below, the same <em>numbers<\/em> and <em>items<\/em> JS array from above will be used to render as react component.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/* react component*\/\nconst numbers = [10, 20, 30, 40, 50];\nconst halves = numbers.map((number) =&gt; \n  &lt;li&gt;number&lt;\/li&gt;\n);\n\n\/\/rendering to DOM\nReactDOM.render(\n  &lt;ul&gt;{halves}&lt;\/ul&gt;,\n  document.getElementById(&#039;root&#039;)\n);\n\/\/OUTPUT\n5\n10\n15\n20\n25<\/code><\/pre>\n<p>In the example above, the JS <code>map()<\/code> function is applied to <code>numbers<\/code> array except that the individual <code>number<\/code> items from the new <code>halves<\/code> array is returned as <code>&lt;li&gt;<\/code> element (line 4).<\/p>\n<p>To render the individual number of <code>halves<\/code> array in react DOM, the entire <code>halves<\/code> array is included inside <code>&lt;ul&gt;<\/code> element (line 9) as JSX expression <code>{}<\/code> and rendered to the DOM (lines: 8-11). The rendered output shows unordered bullet list of <code>halved<\/code> numbers (lines: 13-17).<\/p>\n<p>Similarly, we can use the <code>items<\/code> array from above to iterate individual item using JS <code>map()<\/code> function.<\/p>\n<pre class=\"line-numbers\" data-start=\"18\"><code class=\"language-javascript\">\/* react component- strings*\/\nconst items = [&#039;React&#039;, &#039;JavaScript&#039;, &#039;WordPress&#039;, &#039;Gutenberg&#039;];\nconst newItems = items.map((item) =&gt; \n  &lt;li&gt;item&lt;\/li&gt;\n);\n\n\/\/rendering to DOM\nReactDOM.render(\n  &lt;ul&gt;{newItems}&lt;\/ul&gt;,\n  document.getElementById(&#039;root&#039;)\n);\n\/\/OUTPUT\nReact\nJavaScript\nWordPress\nGutenberg<\/code><\/pre>\n<p>Similar to previous <code>halves<\/code> array, in the example above each <code>item<\/code> from <code>items<\/code> array (line 19) from the new <code>newItems<\/code> array (line 20) are returned as <code>&lt;li&gt;<\/code> element (line 21). Then the <code>newItems<\/code> is passed through ReactDOM to render as <code>{newItems}<\/code> JSX element (line 26) and render it to the DOM (lines: 25-28). The react component renders unordered list of <code>item<\/code> as shown in lines 30-33.<\/p>\n<p>The following flowchart (adopted from <a href=\"https:\/\/medium.com\/byte-sized-react\/component-arrays-in-react-a46e775fae7b\" target=\"_blank\" rel=\"noopener\">Trey Alexender Davis&#8217;s post<\/a>) displays visually how array of data are transformed into react list component.<\/p>\n<figure id=\"attachment_2984\" aria-describedby=\"caption-attachment-2984\" style=\"width: 800px\" class=\"wp-caption alignnone\"><img decoding=\"async\" class=\"size-full wp-image-2984 lazyload\" src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" data-src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2018\/11\/list-flow.png\" alt=\"\" width=\"800\" height=\"262\" \/><figcaption id=\"caption-attachment-2984\" class=\"wp-caption-text\"><noscript><img decoding=\"async\" class=\"size-full wp-image-2984 lazyload\" src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2018\/11\/list-flow.png\" alt=\"\" width=\"800\" height=\"262\" \/><\/noscript> <strong>Figure<\/strong>: A flow chart showing how array of data are transformed into JSX elements and rendered as react component in the DOM.<\/figcaption><\/figure>\n<h6>2. Deep Diving into Basic List Component<\/h6>\n<p>The numbers and items array elements from above can be refactored to a basic list component as shown below. Example adopted from <a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html#basic-list-component\" target=\"_blank\" rel=\"noopener\">Basic List Component<\/a> (React Guide).<\/p>\n<p><strong>Step 1<\/strong>. A very basic react component (functional) syntax.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/functional component syntax\nfunction CompName(props) {\n  \/\/mapping code with &lt;li&gt; element\n  return (\n    \/\/return JSX element with &lt;ul&gt; element\n  );\n}\n\/\/rendering component to DOM\nReactDOM.render(\n  &lt;CompName\/&gt;,\n  document.getElementById(&#039;root&#039;)\n);<\/code><\/pre>\n<p>A basic <a href=\"https:\/\/tinjurewp.com\/jsblog\/learning-reactjs-a-basic-overview\/#functional-component\" target=\"_blank\" rel=\"noopener\">functional (stateless) component<\/a> receives props (line 2) returns JSX element (lines: 4-6). Then the react component is passed to the ReactDOM as first parameter (line 10) and rendered to DOM.<\/p>\n<p><strong>Step 2<\/strong>: Refactoring above <code>numbers<\/code> and items array elements into a basic <code>Numbers<\/code> list component, as shown below:<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/a basic functional component\nfunction Numbers(props) {\n  const numbers = props.numbers;\n  const listedNum = numbers.map((number) =&gt;\n   &lt;li&gt;{number}&lt;\/li&gt;\n  );\n  return (\n    &lt;ul&gt;{listedNum}&lt;\/ul&gt;\n  );\n}\n\nconst numbers = [10, 20, 30, 40];\n\/\/render Numbers Component to DOM\nReactDOM.render(\n  &lt;Numbers numbers={numbers} \/&gt;,\n  document.getElementById(&#039;root&#039;)\n);<\/code><\/pre>\n<p>In the example above, Numbers component receives props (line 2) and <code>numbers<\/code> array data (line 12) are defined as <code>props.numbers<\/code> &amp; stored in <code>numbers<\/code> variable (line 3). The <code>map()<\/code> function is applied to <code>numbers<\/code> array and the individual <code>number<\/code> items from the new <code>listedNum<\/code> array is returned as <code>&lt;li&gt;<\/code> element (line 5). Finally, the new <code>listedNum<\/code> array (lne 4) is returned as <code>&lt;ul&gt;<\/code> list JSX element\u00a0<code>{listedItems}<\/code> with curly braces(line 8).<\/p>\n<p>The <code>numbers<\/code> props declared earlier in line 2 are defined as <code>numbers<\/code> array (line 12) and the <code>Numbers<\/code> component is passed to the ReactDOM as <code>&lt;Numbers numbers={numbers}<\/code> (line 15).<\/p>\n<p>The above code renders unordered bullet list of <code>listedNum<\/code> array (defined in line8). The code throws a warning in browser console (A) &#8211; <em>each child in an array or iterator should have a unique &#8220;key&#8221; prop<\/em>.<\/p>\n<figure id=\"attachment_2961\" aria-describedby=\"caption-attachment-2961\" style=\"width: 929px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-2961 lazyload\" src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" data-src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2018\/11\/list-shot-s.png\" alt=\"\" width=\"929\" height=\"289\" \/><figcaption id=\"caption-attachment-2961\" class=\"wp-caption-text\"><noscript><img decoding=\"async\" class=\"size-full wp-image-2961 lazyload\" src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2018\/11\/list-shot-s.png\" alt=\"\" width=\"929\" height=\"289\" \/><\/noscript> <strong>Figure<\/strong>: Screenshot of <code>Numbers<\/code> component rendered in DOM (A) a missing &#8220;key&#8221; warning (B) and props values (C).<\/figcaption><\/figure>\n<p>The error can be fixed by adding <code>keys.element<\/code> inside the <code>&lt;li&gt;<\/code> element (lines: 5-6). By replacing the codes between lines 5-6 with following codes, the above error can be rectified.<\/p>\n<pre class=\"line-numbers\" data-start=\"5\"><code class=\"language-javascript\">\/\/add key element inside &lt;li. element\n &lt;li key={number.toString()}&gt;\n\t{number}\n\t&lt;\/li&gt;\n  );<\/code><\/pre>\n<h5><strong>Keys in Reacts<\/strong><\/h5>\n<p>To quote from the <a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html#basic-list-component\" target=\"_blank\" rel=\"noopener\">React Guide<\/a> a \u201c<strong>key<\/strong>\u201d is\u00a0 \u201d <em>a special string attribute you need to include when creating lists of elements<\/em>\u201c. Keys are required to identify items that are changed, added or removed from data.\u00a0 Some key features of keys include:<\/p>\n<ul>\n<li>In a data array keys are placed inside <code>&lt;li&gt;<\/code> elements.\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/add key element inside &lt;li. element\n &lt;li key={number.toString()}&gt;\n\t{number}\n\t&lt;\/li&gt;\n  );<\/code><\/pre>\n<\/li>\n<li>Keys serve as a marker only and don\u2019t get pass to components. If IDs are needed in components, it should be passed exclusively as props. The following example from <a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html#keys-must-only-be-unique-among-siblings\" target=\"_blank\" rel=\"noopener\">The React Guide<\/a>:\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/Add IDs explicitly\nconst postsListed = posts.map((post) =&gt;\n  &lt;Post\n    key={post.id}\n    id={post.id}\n    title={post.title} \/&gt;\n);<\/code><\/pre>\n<\/li>\n<li><a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html#keys-must-only-be-unique-among-siblings\" target=\"_blank\" rel=\"noopener\">Keys passed within arrays should be unique<\/a> but not <em>globally<\/em> unique.<\/li>\n<li>Often data IDs are used as keys. The following example from <a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html#keys\" target=\"_blank\" rel=\"noopener\">The React Guide<\/a>:\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/array id as keys\nconst todoItems = todos.map((todo) =&gt;\n  &lt;li key={todo.id}&gt;\n    {todo.text}\n  &lt;\/li&gt;\n);<\/code><\/pre>\n<\/li>\n<li>If there is no stable data IDs, index value of data array can be used but discouraged, especially if order of items is expected to change. The following example from <a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html#keys\" target=\"_blank\" rel=\"noopener\">The React Guide<\/a>:\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/array index as key\nconst todoItems = todos.map((todo, index) =&gt;\n  \/\/ Only do this if items have no stable IDs\n  &lt;li key={index}&gt;\n    {todo.text}\n  &lt;\/li&gt;\n);<\/code><\/pre>\n<\/li>\n<\/ul>\n<h6>Keys &#8211; An Use Case Example<\/h6>\n<p>In the following example, array of <code>items<\/code> code from above is refactored into <code>Items<\/code> list components. A <em>key<\/em> element is added inside the <code>&lt;li&gt;<\/code> element (line 5).<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/basic Items component\nfunction Items(props) {\n  const items = props.items;\n  const listedItems = items.map((item) =&gt;\n   &lt;li key={item.toString()}&gt;\n\t {item}\n\t&lt;\/li&gt;\n  );\n  return (\n    &lt;ul&gt;{listedItems}&lt;\/ul&gt;\n  );\n}\n\nconst items = [&#039;React&#039;, &#039;JavaScript&#039;, &#039;WordPress&#039;, &#039;Gutenberg&#039;];\nReactDOM.render(\n  &lt;Items items={items} \/&gt;,\n  document.getElementById(&#039;main&#039;)\n);<\/code><\/pre>\n<p>The above code renders unordered bullet list of <code>{listedItems}<\/code> JSX element (line 10) from <code>listedItems<\/code> array\u00a0 (ine 4).<\/p>\n<figure id=\"attachment_2966\" aria-describedby=\"caption-attachment-2966\" style=\"width: 930px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-2966 lazyload\" src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" data-src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2018\/11\/list-scrn-2.png\" alt=\"\" width=\"930\" height=\"266\" \/><figcaption id=\"caption-attachment-2966\" class=\"wp-caption-text\"><noscript><img decoding=\"async\" class=\"size-full wp-image-2966 lazyload\" src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2018\/11\/list-scrn-2.png\" alt=\"\" width=\"930\" height=\"266\" \/><\/noscript> <strong>Figure<\/strong>: Screenshot of <code>Items<\/code> component rendered in DOM and passed props values without missing &#8220;keys&#8221; warning.<\/figcaption><\/figure>\n<p>The code with &#8220;keys&#8221; does not throw any warning in the browser console (shown above).<\/p>\n<p>Additional Information: <a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html\" target=\"_blank\" rel=\"noopener\">Lists &amp; Keys | React Guide<\/a><\/p>\n<h4 id=\"list-proof-of-concept\">Posts List Component: A Proof of Concept<\/h4>\n<p>As proof of concepts to create simple posts list component as well as better understand how keys are used in iteration of array items, examples from the <a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html#keys-must-only-be-unique-among-siblings\" target=\"_blank\" rel=\"noopener\">React Guide<\/a> and <a href=\"https:\/\/medium.com\/byte-sized-react\/component-arrays-in-react-a46e775fae7b\" target=\"_blank\" rel=\"noopener\">Trey Davis&#8217;s post<\/a> were referred. This posts contains a heading and list of three posts with a title and some text.<\/p>\n<p>Because this basic list component does not involve any state or life cycle methods, <a href=\"https:\/\/reactjs.org\/docs\/components-and-props.html#function-and-class-components\" target=\"_blank\" rel=\"noopener\">functional (<em>stateless<\/em>) components<\/a> as recommended in <a href=\"https:\/\/reactjs.org\/docs\/components-and-props.html#function-and-class-components\" target=\"_blank\" rel=\"noopener\">React Guide<\/a> were\u00a0 used. Functional components are simple, easy to write &amp; understand.<\/p>\n<h6>Step 1: Add Styling with Basic CSS<\/h6>\n<p>For styling the components, basic styles from\u00a0<a href=\"https:\/\/tinjurewp.com\/jsblog\/learning-to-work-with-react-components\/\" target=\"_blank\" rel=\"noopener\">Learning to Work With React Components<\/a> were copied &amp; used.<\/p>\n<h6>Step 2: Define an Array of Items<\/h6>\n<p>The following <code>posts<\/code> array contains three items each with <code>id<\/code>, <code>title<\/code> and <code>text<\/code> as properties.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ array of posts items\nconst posts = [\n  {\n    id: 1, \n    title: &#039;Hello World&#039;, \n    text: &#039;Welcome to learning React!&#039;\n   },\n  {\n    id: 2, \n    title: &#039;Learn React&#039;, \n    text: &#039;Start learning ReactJs.&#039;\n   },\n  {\n    id: 3, \n    title: &#039;Learn JavaScript&#039;, \n    text: &#039;Start learning JavaScript.&#039;\n   }\n];<\/code><\/pre>\n<h6>Step 3: Define Blog Component to List Posts<\/h6>\n<p>The following\u00a0 <code>Blog<\/code> component with a <strong>props<\/strong> is defined as container component (lines: 19-43) with <code>Header<\/code> and <code>postList<\/code> as child components. The <code>Header<\/code> component (lines: 21-25) returns title.<\/p>\n<p>As described in the previous sections, the <code>map()<\/code> function is applied to <code>posts<\/code> array (lines: 2-18) and the individual <code>post<\/code> items from the new <code>postList<\/code> array is returned as <code>&lt;div&gt;<\/code> JSX element (lines: 29-32) with <code>title<\/code> and <code>text<\/code> as JSX expression (lines: 30-31).<\/p>\n<p>Finally, the <code>Header<\/code> and <code>postList<\/code> are returned as <code>&lt;div&gt;<\/code> list JSX element\u00a0<code>{Header}<\/code> and <code>{postList}<\/code> with curly braces (lines: 36-40)<\/p>\n<pre class=\"line-numbers\" data-start=\"19\"><code class=\"language-javascript\">\/\/ define Blog component\nfunction Blog(props) {\n    const Header = (\n\t&lt;div className=&quot;site-title-text&quot;&gt;\n\t    &lt;h1&gt;Blog Post Listing&lt;\/h1&gt;\n\t &lt;\/div&gt;\t\n\t  );\n \/\/define PostList component\t \n  const postsList = props.posts.map((post) =&gt;\n    &lt;div key={post.id}&gt;\n\t&lt;div className=&quot;entry-content&quot;&gt;\n\t   &lt;h2&gt;{post.title}&lt;\/h2&gt;\n\t   &lt;p&gt;Content: {post.text}&lt;\/p&gt; \n\t &lt;\/div&gt;{\/*.entry-content *\/}\n    &lt;\/div&gt;\n  );\n  return (\n    &lt;div&gt;\n      {Header}\n      &lt;div className=&quot;site-main&quot;&gt; \n        {postsList}\n      &lt;\/div&gt;{\/*.site-main *\/}  \n    &lt;\/div&gt;\n  );\n}<\/code><\/pre>\n<p>A visual explanation of the posts <code>map()<\/code> function in <code>postList<\/code> is shown below.<\/p>\n<figure id=\"attachment_3008\" aria-describedby=\"caption-attachment-3008\" style=\"width: 1000px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-3008 lazyload\" src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" data-src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2018\/11\/blogPost-list.png\" alt=\"\" width=\"1000\" height=\"543\" \/><figcaption id=\"caption-attachment-3008\" class=\"wp-caption-text\"><noscript><img decoding=\"async\" class=\"size-full wp-image-3008 lazyload\" src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2018\/11\/blogPost-list.png\" alt=\"\" width=\"1000\" height=\"543\" \/><\/noscript> <strong>Figure<\/strong>: Screenshot<\/figcaption><\/figure>\n<p>The above flow chart explains how props data are processed and passed to react components. The chart was inspired by Trey Alexander Davis\u2019s <a href=\"https:\/\/medium.com\/byte-sized-react\/handling-data-with-props-in-react-900fdc44a8c\" target=\"_blank\" rel=\"noopener\">Handling Data with Props in React<\/a>.<\/p>\n<h6>Step 4: Render Blog Component<\/h6>\n<p>Finally, the entire <code>Blog<\/code> component is passed to <code>ReactDOM.render()<\/code> function (lines: 45-48) as first parameter where value of <code>posts<\/code> is defined as <code>{post}<\/code> JSX element.<\/p>\n<pre class=\"line-numbers\" data-start=\"44\"><code class=\"language-javascript\">\/\/rendering Blog component to DOM\nReactDOM.render(\n  &lt;Blog posts={posts} \/&gt;,\n  document.getElementById(&#039;main&#039;)\n);<\/code><\/pre>\n<p>The <code>Blog<\/code> component renders unordered list of <code>posts<\/code> and displays in the browser as shown below.<\/p>\n<figure id=\"attachment_3003\" aria-describedby=\"caption-attachment-3003\" style=\"width: 800px\" class=\"wp-caption alignnone\"><img decoding=\"async\" class=\"size-full wp-image-3003 lazyload\" src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" data-src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2018\/11\/scrnshot-list-props-l.png\" alt=\"\" width=\"800\" height=\"452\" \/><figcaption id=\"caption-attachment-3003\" class=\"wp-caption-text\"><noscript><img decoding=\"async\" class=\"size-full wp-image-3003 lazyload\" src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2018\/11\/scrnshot-list-props-l.png\" alt=\"\" width=\"800\" height=\"452\" \/><\/noscript> Figure: Screenshot of rendered <code>Blog<\/code> component in browser showing list of of <code>posts<\/code> array displayed as individual post.<\/figcaption><\/figure>\n<p>Additional Reading: <a href=\"https:\/\/medium.com\/byte-sized-react\/component-arrays-in-react-a46e775fae7b\" target=\"_blank\" rel=\"noopener\">Lists and Arrays in\u00a0React<\/a> by <em>Trey Alexander Davis<\/em> &amp; <a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html#keys-must-only-be-unique-among-siblings\" target=\"_blank\" rel=\"noopener\">React Guide<\/a><\/p>\n<h5>Wrapping Up<\/h5>\n<p>In this part 3 of the three-part essential prerequisite post series, how list of items in array are transform into list of <a href=\"https:\/\/reactjs.org\/docs\/rendering-elements.html\" target=\"_blank\" rel=\"noopener\">React element<\/a> were discussed with with use case examples. In the next post, how to create a simple blog posts listing and refactoring of codes into smaller components will be discussed.<\/p>\n<p><em>Acknowledgements<\/em>: This post was inspired by <em>Trey A. Davis<\/em>&#8216;s <a href=\"https:\/\/medium.com\/byte-sized-react\/component-arrays-in-react-a46e775fae7b\" target=\"_blank\" rel=\"noopener\">Lists and Arrays in\u00a0Rea<\/a> post and <a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html#keys-must-only-be-unique-among-siblings\" target=\"_blank\" rel=\"noopener\">React Guide<\/a> documentation.<\/p>\n<p>Next Post: Creating a Simple Blog Listing React Component<\/p>\n<h5>Useful Resources and Links<\/h5>\n<p>While preparing this post, I have referred the following references extensively. Please to refer original posts for more detailed information.<\/p>\n<ul>\n<li><a href=\"https:\/\/medium.com\/byte-sized-react\/component-arrays-in-react-a46e775fae7b\" target=\"_blank\" rel=\"noopener\">Lists and Arrays<\/a> | <a href=\"https:\/\/medium.com\/byte-sized-react\" target=\"_blank\" rel=\"noopener\">Byte-sized React<\/a><\/li>\n<li><a href=\"https:\/\/www.andreasreiterer.at\/react-list-component\/\" target=\"_blank\" rel=\"noopener\">How To Create a React List Component<\/a> | <a href=\"https:\/\/www.andreasreiterer.at\/\" target=\"_blank\" rel=\"noopener\">Andreas Reiterer<\/a><\/li>\n<li><a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html#keys-must-only-be-unique-among-siblings\" target=\"_blank\" rel=\"noopener\">List and Keys<\/a> | <a href=\"https:\/\/reactjs.org\/docs\/lists-and-keys.html\" target=\"_blank\" rel=\"noopener\">React Guide<\/a><\/li>\n<li><a href=\"https:\/\/css-tricks.com\/render-children-in-react-using-fragment-or-array-components\/\" target=\"_blank\" rel=\"noopener\">Render Children in React Using Fragment or Array\u00a0Components<\/a> | <a href=\"https:\/\/css-tricks.com\/\" target=\"_blank\" rel=\"noopener\">CSS-TRICKS<\/a><\/li>\n<li><a href=\"http:\/\/jasonjl.me\/blog\/2015\/04\/18\/rendering-list-of-elements-in-react-with-jsx\/\" target=\"_blank\" rel=\"noopener\">Rendering List of Elements in React With JSX<\/a> | <a href=\"http:\/\/jasonjl.me\/\" target=\"_blank\" rel=\"noopener\">jasonjl.me<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Note: This is part 3 of three-part prerequisite post series. A\u00a0 prior knowledge of these topics is essential before deep diving into Creating List React component. In the previous learning-note post\u00a0Learning to Work With React Components, how to build a simple react component using run time &lt;script&gt;in a HTML file were discussed. Although slow, this [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[4,5,40],"tags":[],"class_list":["post-2719","post","type-post","status-publish","format-standard","hentry","category-javascript","category-reactjs","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts\/2719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/comments?post=2719"}],"version-history":[{"count":0,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts\/2719\/revisions"}],"wp:attachment":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/media?parent=2719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/categories?post=2719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/tags?post=2719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}