Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

usePresence: updateStatus changes every render #36

Closed
shshaw opened this issue Apr 6, 2023 · 1 comment · Fixed by #37
Closed

usePresence: updateStatus changes every render #36

shshaw opened this issue Apr 6, 2023 · 1 comment · Fixed by #37

Comments

@shshaw
Copy link

shshaw commented Apr 6, 2023

With usePresence, I was trying to call updateStatus when user information changes, but dropping it in a useEffect hook was triggering it in a loop since the updateStatus function changes every render.

Can that function be wrapped in useCallback() or something to prevent it from triggering updates?

Example of the problem:

const [userData] = useUserData();
const [users, updateStatus] = usePresence('channel-name');

useEffect(()=>{
  if ( userData ) updateStatus(userData);
},[userData, updateStatus]); // Infinite loop!

The "solution" right now is to just leave updateStatus off the dependency array, but that's not ideal since linters and React complain about "exhaustive dependencies".

useEffect(()=>{
  if ( userData ) updateStatus(userData);
},[userData]); // No infinite loop
@owenpearson
Copy link
Contributor

Hey @shshaw, we have update the library so that updateStatus is now wrapped in a useCallback, please try out the new version (2.1.1) and let us know if you have any further issues 🙂

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants