fix(lint/useExhaustiveDependencies): correct fix for method calls#7709
fix(lint/useExhaustiveDependencies): correct fix for method calls#7709
Conversation
🦋 Changeset detectedLatest commit: 5abba2b The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Walkthrough
Possibly related PRs
Suggested labels
Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
CodSpeed Performance ReportMerging #7709 will not alter performanceComparing Summary
Footnotes
|
|
Hi @siketyan , Thanks for this PR. I agree with the rest of the changes, but can you please explain why granular dependencies are now invalid? I'm referring to the following example: This is a very common pattern, and without this pattern, the effect would run when any prop changes, and not just |
|
@AfrazHussain Actually calling |
|
Agree with @AfrazHussain. The "fix" caused a regression in behavior. And after modifying foo.bar: But it should have been: In render 2a, it's assumed that the code author is using immutability. This is backed up by ESLint's behavior as seen here on this playground link: You'll notice that it will be silenced if you add either |
|
Thank you. Perhaps my report is misplaced then. I will open a new ticket, since I'm seeing regressions related to what was changed here, although it may not be exactly the same ( Opened a new issue here: #7782 |
|
@scarlac , Thanks for creating your issue. That is in fact what my comment was referring to as well like you correctly pointed out initially. @siketyan The issue (facebook/react#16265) that you've linked is an open issue for the past 6 years, and even there people have mentioned work-arounds for this behaviour, so I don't think that if its the behaviour of |
|
I’ve mentioned this previously in the Biomejs Discord, but it’s worth documenting here for future reference. This is a concrete example of why methods can be unsafe as hook dependencies. I've encountered it a couple days ago at my workplace. function Test() {
const [values, setValues] = useState<string[]>([]);
const computed = useMemo(
() => values.map((v) => v.toUpperCase()),
[
// Expected: `values`, since it is used to derive a new array.
// Proposed by Biome:
values.map
// This would always evaluate as stable because
// `Array.prototype.map === Array.prototype.map`,
// even when `values` changes.
],
);
} |
Summary
The useExhaustiveDependencies rule now correctly adds the object as a dependency when its method is called within a React hook. It should be avoided to add the method itself as a dependency because it will break the prototype chain.
Test Plan
Added a snapshot test.
Docs
N/A