-
-
Notifications
You must be signed in to change notification settings - Fork 508
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
💅 React prop is considered an unnecessary dependency to React.useMemo #4248
Comments
Note: lint/complexity/noUselessFragments is disabled because of biomejs/biome#4247 Also follow biomejs/biome#4248
i'd like to work on this~ |
@jennydaman -- are you sure that your example should pass? ESLint's React Hook lints fail it too-- but perhaps its error message is better. Does its error message make sense to you? Passing a function that not an in-line anonymous function to useMemo does seem rather odd to me.
|
I think it should. Consider: import React from "react";
type ExampleProps = {
getData: () => string;
};
const ExampleComponent: React.FC<ExampleProps> = ({ getData }) => {
const data = React.useMemo(getData, []); // <-- empty dependency array!
return <span>{data}</span>;
};
const ParentComponent = () => {
const [fn, setFn] = React.useState(() => "first");
return (
<>
<ExampleComponent getData={fn} />
<button
type="button"
onClick={setFn(() => "second")}
>
click me
</button>
</>
);
}; Since the dependency array is empty,
Not really, but here's what I found: https://stackoverflow.com/a/69830942 What I think ESLint is trying to say is, it is unable to lint |
I have to admit I'm not seeing the purpose in fixing this issue. It seems like a rather strange edge case, and it's generally very standard to pass an ad-hoc anonymous function to |
Environment information
Rule name
lint/correctness/useExhaustiveDependencies
Playground link
https://biomejs.dev/playground/?code=aQBtAHAAbwByAHQAIABSAGUAYQBjAHQAIABmAHIAbwBtACAAIgByAGUAYQBjAHQAIgA7AAoACgB0AHkAcABlACAARQB4AGEAbQBwAGwAZQBQAHIAbwBwAHMAIAA9ACAAewAKAAkAZwBlAHQARABhAHQAYQA6ACAAKAApACAAPQA%2BACAAcwB0AHIAaQBuAGcAOwAKAH0AOwAKAAoAYwBvAG4AcwB0ACAARQB4AGEAbQBwAGwAZQBDAG8AbQBwAG8AbgBlAG4AdAA6ACAAUgBlAGEAYwB0AC4ARgBDADwARQB4AGEAbQBwAGwAZQBQAHIAbwBwAHMAPgAgAD0AIAAoAHsAIABnAGUAdABEAGEAdABhACAAfQApACAAPQA%2BACAAewAKAAkAYwBvAG4AcwB0ACAAZABhAHQAYQAgAD0AIABSAGUAYQBjAHQALgB1AHMAZQBNAGUAbQBvACgAZwBlAHQARABhAHQAYQAsACAAWwBnAGUAdABEAGEAdABhAF0AKQA7AAoACQByAGUAdAB1AHIAbgAgADwAcwBwAGEAbgA%2BAHsAZABhAHQAYQB9ADwALwBzAHAAYQBuAD4AOwAKAH0AOwAKAA%3D%3D
Expected result
The biome explanation
Is incorrect. When a React prop is changed, the component is re-rendered.
Props should be specified in the dependency array for
React.useMemo
.Code of Conduct
The text was updated successfully, but these errors were encountered: