fix: stop infinite rerenders by removing query from useEffect deps#88
Merged
fix: stop infinite rerenders by removing query from useEffect deps#88
Conversation
Contributor
Author
|
Side note: our code style enforcement is too good - this breaks an eslint rule, so before I explicitly disabled that rule for this line |
varl
approved these changes
Aug 25, 2019
dhis2-bot
pushed a commit
that referenced
this pull request
Aug 25, 2019
## [1.5.1](v1.5.0...v1.5.1) (2019-08-25) ### Bug Fixes * stop infinite rerenders by removing query from useEffect deps ([#88](#88)) ([ac9fa28](ac9fa28))
Contributor
|
🎉 This PR is included in version 1.5.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
SferaDev
reviewed
Aug 25, 2019
| // Cleanup inflight requests | ||
| return abort | ||
| }, [context, query, refetchCount]) | ||
| }, [context, refetchCount]) // eslint-disable-line react-hooks/exhaustive-deps |
There was a problem hiding this comment.
@amcgee Query should be mutable or immutable by design?
Instead of a deep equality check, a ref could be used instead.
If mutable it just holds initial value inside and if mutable it updates the ref with actual value as a side effect.
And in the dependency array you can now safely use the ref as it won't change its object reference.
function Example(query) {
const request = useRef(query);
// If immutable just remove the side effect that updates
useEffect(() => {
request.current = query;
});
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
IMPORTANT FIX
A (sort of) bug was introduced in #34 - we started re-fetching whenever the query changed, but since many queries are object literals constructed within the render loop, their reference identity would actually change and cause an infinite loop of re-renders and re-fetches. @Mohammer5 noticed this in the new maintenance app.
The true "correct" solution might be to do a deep equality check on the query, but I think this is a good solution for now - we don't list
queryas a dep ofuseEffect, even though it technically is. This means nothing will happen if the query is updated after the hook is first run by the render method, which should be fine.