<?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: Mark Cockram</title>
    <description>The latest articles on DEV Community by Mark Cockram (@nubebuster).</description>
    <link>https://dev.to/nubebuster</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%2F977976%2Fd782c313-8783-4c8e-9245-4dd035564006.jpeg</url>
      <title>DEV Community: Mark Cockram</title>
      <link>https://dev.to/nubebuster</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nubebuster"/>
    <language>en</language>
    <item>
      <title>Prevent reinitialization of React tooltip component to prevent API calls</title>
      <dc:creator>Mark Cockram</dc:creator>
      <pubDate>Wed, 23 Nov 2022 20:32:40 +0000</pubDate>
      <link>https://dev.to/nubebuster/prevent-reinitialization-of-react-tooltip-component-to-prevent-api-calls-105i</link>
      <guid>https://dev.to/nubebuster/prevent-reinitialization-of-react-tooltip-component-to-prevent-api-calls-105i</guid>
      <description>&lt;p&gt;I load profile data from API to be displayed when the client hovers over a profile name. When the client stops hovering and then hovers again, the tooltip component gets reinitialized and an API call is done again. Currently I am using localStorage to cache the data but this seems like an improper fix.&lt;/p&gt;

&lt;p&gt;My question is how do I prevent the tooltip component from being thrown away and reinitialized?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2ftvo1fbpn3rr8xqy5x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2ftvo1fbpn3rr8xqy5x.png" alt="Image description" width="216" height="381"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const renderTooltip = (props) =&amp;gt; (
    &amp;lt;Tooltip&amp;gt;
      &amp;lt;UserInfoFetchData uuid={props} /&amp;gt;
    &amp;lt;/Tooltip&amp;gt;
  );

  &amp;lt;OverlayTrigger
    placement="top"
    uuid={gamer.uuid}
    delay={{ show: 250, hide: 400 }}
    overlay={renderTooltip(gamer.uuid)}
  &amp;gt;
      &amp;lt;a href={"/user/" + gamer.uuid}&amp;gt;{gamer.username}&amp;lt;/a&amp;gt;
  &amp;lt;/OverlayTrigger&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code is used to call the React Component. Below you see the code of the component itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useState } from "react";
import Backend from "../../data/Backend";

export default class UserInfoFetchData extends React.Component {
  constructor(props) {
    super(props);
    console.log("test");
    this.state = {};
  }

  componentDidMount() {
    const cacheLocation = "profile " + this.props.uuid;
    let data = localStorage.getItem(cacheLocation);

    if (!data) {
      this.controller = new AbortController();
      new Backend()
        .getUser({ uuid: this.props.uuid }, { signal: this.controller.signal })
        .then((response) =&amp;gt; {
          console.log("fetched data");
          this.setState(response.data);
          localStorage.setItem(cacheLocation, JSON.stringify(response.data));
          this.controller = null;
        });
    } else {
      this.setState(JSON.parse(data));
    }
  }

  componentWillUnmount() {
    if (this.controller) {
      this.controller.abort();
    }
  }

  render() {
    const capitalize = (str) =&amp;gt; {
      return `${str[0].toUpperCase()}${str.slice(1)}`;
    };
    return (
      &amp;lt;div className="card border-dark "&amp;gt;
        &amp;lt;div className="card-body"&amp;gt;
          &amp;lt;table className="table table-hover"&amp;gt;
            &amp;lt;tbody&amp;gt;
              {Object.keys(this.state)
                .filter(
                  (o) =&amp;gt;
                    o !== "uuid" &amp;amp;&amp;amp;
                    o !== "username" &amp;amp;&amp;amp;
                    !o.includes("weekly") &amp;amp;&amp;amp;
                    !o.includes("monthly")
                )
                .map((e, i) =&amp;gt; {
                  return (
                    &amp;lt;tr key={e}&amp;gt;
                      &amp;lt;th scope="row"&amp;gt;{capitalize(e.replace("_", " "))}&amp;lt;/th&amp;gt;

                      &amp;lt;td&amp;gt;{this.state[e]}&amp;lt;/td&amp;gt;
                    &amp;lt;/tr&amp;gt;
                  );
                })}
            &amp;lt;/tbody&amp;gt;
          &amp;lt;/table&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The console logs "test" each time I hover over a username, which means the component is being reinitialized each time. Is there a way I can make this only initialize once? Or is there a way to properly store the API retrieved data in the class?&lt;/p&gt;

&lt;p&gt;Note: new Backend().getUser() simply returns an AxioInstance.&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
