YggY
Reactiflux3y ago
9 replies
Ygg

✅ – Ygg – 22-32 Mar 8

Hello, I'm using a
FreeCompanyContext
to use some variables across my website
One of then is using a
function
to fetch a list of objects from an api.

The list can be lenghty so I decided to track the progress to show it in the UI :
  let [fetchProgress, setFetchProgress] = useState(0);

  async function fetchMembersData() {
    setMembersFetchLoad(true);
    const result = (await getCharacterList(FreeCompanyMembers, (progress) => {
      setFetchProgress(Math.trunc(progress));
    })) as CharacterData[];
    setMembersFullData(result);
    setMembersFetchLoad(false);
  }


And then I'm using it in the component:
          const { ..., fetchProgress } = useFreeCompanyContext();
          ...
          <button className="btn loading">
            Getting members info ({fetchProgress}%)
          </button>


Everything works just fine, except that
fetchProgress
is a
useState
so it re-renders everytime it changes and that make the whole page a mess to use (since it's re-rendering the whole page)
The gif is just to show the
:hover
effect going crazy because of that, this re-renders also make it harder to click the tabs.

So, my question: what's the best approach to this?

I'm a newbie so any obvious suggestions could end up being great!
ezgif-4-e1fb357c06.gif
Was this page helpful?