Sitelet https://web.archive.org/web/20200616125917/https://github.com/mui-org/material-ui/issues/20442
Skip to content
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

handleTabsScroll uses useCallback without passing arguments #20442

Open
cubbie opened this issue Apr 6, 2020 · 7 comments
Open

handleTabsScroll uses useCallback without passing arguments #20442

cubbie opened this issue Apr 6, 2020 · 7 comments

Comments

@cubbie
Copy link

@cubbie cubbie commented Apr 6, 2020 •

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

debug.module.js:229 In ForwardRef(Tabs) you are calling useMemo/useCallback without passing arguments.
This is a noop since it will not be able to memoize, it will execute it every render.

also calling useEffect on line 349 of Tabs.js with no second argument as well.

Expected Behavior 🤔

Pass an additional second argument or remove the useCallback method as it creates an unnecessary wrapper.

Steps to Reproduce 🕹

Steps:

n/a

Context 🔦

Remove error messages.

Your Environment 🌎

Tech Version
Material-UI v4.9.7
Preact 10.3.4
TypeScript 3.7.5
@eps1lon
Copy link
Member

@eps1lon eps1lon commented Apr 6, 2020

Good catch. I wonder why the linter doesn't complain.

const handleTabsScroll = React.useCallback(
debounce(() => {
updateScrollButtonState();
}),
);

also calling useEffect on line 349 of Tabs.js with no second argument as well.

This is intended.

@oliviertassinari
Copy link
Member

@oliviertassinari oliviertassinari commented Apr 6, 2020 •

So the solution would be?

diff --git a/packages/material-ui/src/Tabs/Tabs.js b/packages/material-ui/src/Tabs/Tabs.js
index b59620fb1..97b0021e4 100644
--- a/packages/material-ui/src/Tabs/Tabs.js
+++ b/packages/material-ui/src/Tabs/Tabs.js
@@ -334,6 +334,7 @@ const Tabs = React.forwardRef(function Tabs(props, ref) {
     debounce(() => {
       updateScrollButtonState();
     }),
+    [],
   );

   React.useEffect(() => {

@cubbie How did you spot it?

@cubbie
Copy link
Author

@cubbie cubbie commented Apr 6, 2020

I had that warning pop up from Preact. Probably should remove useCallback, because it makes no sense to use this method with an empty array, it will create an additional wrapper which wont increase performance (at least I think? I could be wrong)

@oliviertassinari
Copy link
Member

@oliviertassinari oliviertassinari commented Apr 6, 2020 •

@cubbie Awesome to see the library used with Preact :D. I wish React had such warning, how comes they don't 😮. @eps1lon a potential opportunity for a contribution to React? ;)

Regarding the best option, removing vs keeping. I honestly don't know. Either way sounds great. @eps1lon What's your preference?

@NMinhNguyen
Copy link
Contributor

@NMinhNguyen NMinhNguyen commented Apr 6, 2020

I think in this instance it will have to be kept because debounce creates a function. And the reason the ESLint plugin doesn't complain is because it only expects to be passed something it can statically analyse: a function (or an arrow function). It can't know what the result of calling debounce would be :)

@eps1lon
Copy link
Member

@eps1lon eps1lon commented Apr 7, 2020

It can't know what the result of calling debounce would be :)

But in 99.9% of the cases you'll use the second argument anyway. That should be the default. There are use cases to omit them for useMemo/useCallback but in those cases you're on your own and should disable the rule.

@NMinhNguyen
Copy link
Contributor

@NMinhNguyen NMinhNguyen commented Apr 7, 2020 •

I actually can't think of the 0.01% that you'd not want a dependency array when using useMemo/useCallback, but yeah, maybe this discussion can be moved to the React core repo, and we just add []

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.