<?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: Isaac de Oliveira Almeida</title>
    <description>The latest articles on DEV Community by Isaac de Oliveira Almeida (@yzap).</description>
    <link>https://dev.to/yzap</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%2F2143041%2F04ca06ff-e474-403d-a20a-21d5221b4ce4.jpg</url>
      <title>DEV Community: Isaac de Oliveira Almeida</title>
      <link>https://dev.to/yzap</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yzap"/>
    <language>en</language>
    <item>
      <title>[Help] React OnClick on element</title>
      <dc:creator>Isaac de Oliveira Almeida</dc:creator>
      <pubDate>Mon, 30 Sep 2024 12:06:30 +0000</pubDate>
      <link>https://dev.to/yzap/help-react-onclick-on-element-46mg</link>
      <guid>https://dev.to/yzap/help-react-onclick-on-element-46mg</guid>
      <description>&lt;p&gt;I just found out that when you give an element an OnClick attribute with any function, it doesn't store the reference to the function. Instead, it stores the version of the function at that current time. See:&lt;br&gt;
`  const showLength = () =&amp;gt; {&lt;br&gt;
    console.log(a.length);&lt;br&gt;
  };&lt;/p&gt;

&lt;p&gt;const [a, setA] = useState([&lt;br&gt;
    &lt;/p&gt;
&lt;li&gt;,
  ]);

&lt;p&gt;const add = (event) =&amp;gt; {&lt;br&gt;
    setA([...a, &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;]);
    console.log(a);
  };
  return (
    &lt;ul&gt;

      &lt;li&gt;

        add
        showLength
      &lt;/li&gt;

      {a}
    &lt;/ul&gt;

  );
};
`

&lt;p&gt;The output of this is a square, next to two buttons: add, and showLength&lt;/p&gt;

&lt;p&gt;Add increases the list and shows another square next to the first one, and showLength shows the current length of the list.&lt;/p&gt;

&lt;p&gt;My problem is that the first square also recieved the onClick function showLength, but it always prints "1". Does anyone know how to fix this?&lt;/p&gt;
&lt;/li&gt;

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