-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
Be explicit about side effect dependencies #366
Changes from 11 commits
e7182a5
8b071da
51963bf
8666a05
ce215a9
dbcebf8
3089cda
dfa7e9e
dc96058
54744ab
d80f2d5
ee29ef4
8b2426a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,10 +157,11 @@ export function Issues() { | |
debug().warn('IssuesControl#Issues: 2, no repo defined') | ||
return | ||
} | ||
const fetchComments = async (selectedIssue) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no longer async method - why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function calls The called function blocks (via use of the Given that it is blocking, there is nothing asynchronous about this function that requires it to be tagged with the |
||
|
||
const fetchComments = ((selectedIssue) => { | ||
try { | ||
const commentsArr = [] | ||
const commentsData = await getComments(repository, selectedIssue.number) | ||
const commentsData = getComments(repository, selectedIssue.number) | ||
if (commentsData) { | ||
commentsData.map((comment) => { | ||
commentsArr.push({ | ||
|
@@ -178,18 +179,17 @@ export function Issues() { | |
} catch { | ||
debug().log('failed to fetch comments') | ||
} | ||
} | ||
if (selectedIssueId !== null) { | ||
fetchComments(filteredIssue) | ||
} | ||
}) | ||
|
||
// This address bug #314 by clearing selected issue when new model is loaded | ||
if (!filteredIssue) { | ||
if (filteredIssue !== null) { | ||
fetchComments(filteredIssue) | ||
} else { | ||
setSelectedIssueId(null) | ||
} | ||
// this useEffect runs everytime issues are fetched to enable fetching the comments when the platform is open | ||
// using the link | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [selectedIssueId, issues, repository]) | ||
}, [filteredIssue, repository, setComments, setSelectedIssueId]) | ||
|
||
|
||
return ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oogali why is it better to wrap location and navigation hooks with useRef?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you call
useEffect
, you are expected to declare your dependencies: the things that should trigger when your side effect is executed. This invocation ofuseEffect
relies on the values of the history and location objects provided by React Router.However, if one lists
useNavigate
as a direct dependency, then you end up with a render loop when the underlying React code attempts to compare the value of all dependencies pre- and post-render. Keep in mind, that if any of the side effect's dependencies is a function (such asuseNavigate
), it will execute it and store its result for future comparisons.The ultimate result is the initial render calls your side-effect hook, then React evaluates your dependencies which results in a call to
useNavigate()
, which under the hood triggers a re-render, which then triggers your side-effect hook, so on and so forth.To avoid this, we use
useRef
to store a reference (or one can call it a pointer) to the history and navigation objects, and list the references as our dependencies. With this method, React is comparing the values returned by the React Router hooks rather than entering the infinite execution loop.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh that makes sense!