<?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: EVO-9</title>
    <description>The latest articles on DEV Community by EVO-9 (@evo9).</description>
    <link>https://dev.to/evo9</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%2F588422%2Fe748e885-4bcc-4b1d-a4fa-068da4310d81.png</url>
      <title>DEV Community: EVO-9</title>
      <link>https://dev.to/evo9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/evo9"/>
    <language>en</language>
    <item>
      <title>Using a hook inside .map</title>
      <dc:creator>EVO-9</dc:creator>
      <pubDate>Mon, 01 Mar 2021 12:25:25 +0000</pubDate>
      <link>https://dev.to/evo9/using-a-hook-inside-map-2f37</link>
      <guid>https://dev.to/evo9/using-a-hook-inside-map-2f37</guid>
      <description>&lt;p&gt;Hi&lt;/p&gt;

&lt;p&gt;Firstly, apologies if my terms are not correct.&lt;/p&gt;

&lt;p&gt;I am learning React and I have created a simple accordion component. This works as it should but I have found an issue trying to make it reusable with arrays that are structured differently.&lt;/p&gt;

&lt;p&gt;I have managed to use a hook which allows me to either use my blogs.json file or my stories.json 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;&amp;lt;Accordion array={stories}/&amp;gt;
&amp;lt;Accordion array={blogs}/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now my issue is, my Accordion component uses .map to gather the information, I have asked it to look for i.e. item.content but the blogs array doesn't have content, the blogs array uses body.&lt;/p&gt;

&lt;p&gt;I tried using the following:&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;Accordion array={stories} content={stories.content}/&amp;gt;
&amp;lt;Accordion array={blogs} content={stories.body}/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but that won't work. I was thinking I need to change the following line in the component&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;p&amp;gt;{item.content}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to something like:&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;p&amp;gt;{content}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which I can pass some static text and it work fine but nothing from the array.&lt;/p&gt;

&lt;p&gt;Here's my component,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect, useRef } from "react";
import styled from "styled-components";
import stories from "../data/Stories.json";

const Accordion = ({ array }) =&amp;gt; {
  const [activeIndex, setActiveIndex] = useState(null);
  const ref = useRef();

  useEffect(() =&amp;gt; {
    const onBodyClick = (event) =&amp;gt; {
      if (ref.current &amp;amp;&amp;amp; ref.current.contains(event.target)) {
        return;
      }
    };
    document.body.addEventListener("click", onBodyClick);
    return () =&amp;gt; {
      document.body.removeEventListener("click", onBodyClick);
    };
  }, []);

  const handleChange = (index) =&amp;gt; {
    setActiveIndex(activeIndex === index ? null : index);
  };

  const renderedItems = array.map((item, index, key) =&amp;gt; {
    return (
      &amp;lt;div key={item.title}&amp;gt;
        &amp;lt;div
          ref={ref}
          className={`${activeIndex === index ? "active" : ""} title`}
          onClick={() =&amp;gt; handleChange(index)}
        &amp;gt;
          &amp;lt;i className="dropdown icon"&amp;gt;&amp;lt;/i&amp;gt;
          {item.title}
        &amp;lt;/div&amp;gt;
        &amp;lt;div className={`${activeIndex === index ? "active" : ""} content`}&amp;gt;
          &amp;lt;p&amp;gt;{item.content}&amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  });
  return &amp;lt;div className="ui styled accordion"&amp;gt;{renderedItems}&amp;lt;/div&amp;gt;;
};

export default Accordion;

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

&lt;/div&gt;



&lt;p&gt;How do I overcome this?&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
