Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Infinite renders/calls for using useSubscribeToChanges #113

Conversation

Nirzhuk
Copy link
Contributor

@Nirzhuk Nirzhuk commented Nov 11, 2024

If we use the same example from the documentation we will have an permanent re-rendering per tick which causes a huge memory leak which with 2 of the same, will crash any app.

Why is this caused?

update function is revalue it due useSubscribeToChanges because update have an setResult so the useEffect for the update will trigger infinite without even subscribing.

Solution:
Removing update from the dependencies from useEffect will only trigger 1 time so we can have initial values, then we depends totally from useSubscribeToChanges to trigger update which is the porpuse of the subscribing hook.

@Nirzhuk Nirzhuk changed the title Fix Infinite calls for useSubscribeToChanges Fix Infinite renders/calls for using useSubscribeToChanges Nov 11, 2024
@robertherber robertherber merged commit ac33edc into kingstinct:master Nov 13, 2024
0 of 2 checks passed
@robertherber
Copy link
Member

@Nirzhuk I realized this should be reverted since it breaks the rules of hooks (the linting workflow had fallen out of grace for some reason).

The way to do this is ensuring that you don't send in new parameters on every render (i.e. make sure the dates you're sending in are stable and not changing all the time).

@Nirzhuk
Copy link
Contributor Author

Nirzhuk commented Nov 25, 2024

@Nirzhuk I realized this should be reverted since it breaks the rules of hooks (the linting workflow had fallen out of grace for some reason).

The way to do this is ensuring that you don't send in new parameters on every render (i.e. make sure the dates you're sending in are stable and not changing all the time).

I feel the rules of hooks are not specify the way you can control your hooks, useEffect(call,[]) only needs to triggers on render, which initialize our data after that useSubscribeToChanges gets all the responsability to get the info.
And rules of hooks specify you can use them to trigger in the point you need of the life of the component (which in this case is the creation).

I tried to use your example code but simplied (without the map) and got the same result. I understand your point tho. Thanks for writing a comment about it.

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

Successfully merging this pull request may close these issues.

2 participants