Skip to content

chore: avoid react linting errors (attempt #2) - #85

Merged
nzakas merged 2 commits into
eslint:mainfrom
patricktree:chore/avoid-react-linting-errors-attempt-2
Mar 3, 2025
Merged

nzakas merged 2 commits into
eslint:mainfrom
patricktree:chore/avoid-react-linting-errors-attempt-2

Conversation

@patricktree

Copy link
Copy Markdown
Contributor

Prerequisites checklist

What is the purpose of this pull request?

Attempt #2 (after #83) to unblock #70.
By avoiding the 2 linting errors already present in the codebase.

What changes did you make? (Give an overview)

This time, I did not introduce any behavior changes.
I just replaced useCallback by useMemo because then the warning React Hook useCallback received a function whose dependencies are unknown vanishes.

  • chore: disable warning for useEffect for code path on mount
  • chore: avoid warning by replacing useCallback by useMemo

Related Issues

Is there anything you'd like reviewers to focus on?

@netlify

netlify Bot commented Mar 1, 2025 •

Copy link
Copy Markdown

✅ Deploy Preview for eslint-code-explorer ready!

Name Link
🔨 Latest commit 45eef42
🔍 Latest deploy log https://app.netlify.com/sites/eslint-code-explorer/deploys/67c30ded596edb0008057ea2
😎 Deploy Preview https://deploy-preview-85--eslint-code-explorer.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@patricktree

Copy link
Copy Markdown
Contributor Author

I tested that it does not cause issues when typing quickly into the editor (was the problem with #83).

@nzakas nzakas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Thanks.

@nzakas
nzakas merged commit a7b2a22 into eslint:main Mar 3, 2025
Comment thread src/components/editor.tsx
() =>
debounce((value: string) => {
onChange?.(value);
}, 400),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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


const debouncedOnChange = useMemo(
		() =>
			debounce((value: string) => {
				onChange?.(value);
			}, 400),
		[onChange],
	);
	

How does useMemo help in this case? We are returning a function, not a value. useCallback is preferred when using a function. In this case, if we use useMemo, it wouldn't have any impact, right? Is my understanding correct, or am I missing something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Functionally, it is equivalent, see also https://react.dev/reference/react/useCallback#how-is-usecallback-related-to-usememo
But for whatever reason, the react eslint plugin would warn when using useCallback:

React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead. eslint(react-hooks/exhaustive-deps)

That error vanishes with useMemo.

@patricktree
patricktree deleted the chore/avoid-react-linting-errors-attempt-2 branch March 3, 2025 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants