Skip to content

Conversation

@jberdine
Copy link
Contributor

No description provided.

Copy link
Contributor Author

@jberdine jberdine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I mentioned in the meeting where our example code seems to be unidiomatic. What do others think?

// Specify no dependencies to react since the callback is meant to only
// execute once to fetch the initial data.
// eslint-disable-next-line react-hooks/exhaustive-deps
[],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, the linter wants

Suggested change
[],
[skipclient],

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it is not clear to me that it is ok to execute the body of the useEffect more than once. Here the proposed dep skipclient is also constant, so maybe it doesn't matter. I am unsure, are others confident?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for the other comment thread -- I think the linter is right that skipclient is not necessarily constant in general, as it comes from React's useContext. And if that Skip Client context changes, then it would be right to reevaluate this effect, so I think the linter-proposed dependency array looks right. But confirmation from Daniel would be good :)

// Specify no dependencies to react since reevaluation of the callback
// is triggered after a timeout by setInterval.
// eslint-disable-next-line react-hooks/exhaustive-deps
[],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, the linter wants

Suggested change
[],
[post_id],

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this case, my understanding of react (which could be wrong!) is that adding the post_id dep here would have the effect of triggering reevaluation sooner than the timeout in case post_id changes. But, and maybe I'm confused, isn't post_id constant, and so it makes no difference? Either way, it seems ok to me to add the dep here and appease the linter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's my understanding of react/useEffect semantics here as well. But, post_id comes from useParams, which is React's way of accessing URL parameters and thus not necessarily constant -- so I think it's right to specify it as a dependency here.

The code looks similar enough to this example from the react docs that I suspect that the empty deps array is a relic of some earlier version that was set up that same way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks, I will change this. I am fundamentally confused by react. What I expect is that at the point that useEffect is called, the function argument gets evaluated to a closure that binds post_id, which has type number and is already evaluated. So I don't see how react can treat it as anything other than a constant number value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Within a single render, yes, post_id is a constant number. But if the params change, that will trigger a re-render with some new post_id!

@bennostein
Copy link
Contributor

bennostein commented Oct 25, 2024

LGTM -- the linter-preferred version seems better to me, unless @skiplabsdaniel sees some problem

@jberdine jberdine changed the title Suppress react-hooks/exhaustive-deps in hackernews example Add deps to react useEffect calls in hackernews example Oct 28, 2024
@jberdine jberdine merged commit 2786918 into SkipLabs:main Oct 28, 2024
3 checks passed
@jberdine jberdine deleted the react-deps branch October 28, 2024 14:08
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