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

[core] Make useForkRef variadic #27939

Merged
merged 6 commits into from Oct 6, 2022
Merged

Conversation

michaldudak
Copy link
Member

@michaldudak michaldudak commented Aug 24, 2021

This allows useForkRef to be called with more than two parameters, eliminating the need for one-time use variables and improving readability of the code.

Closes #34383

@michaldudak michaldudak added the core Infrastructure work going on behind the scenes label Aug 24, 2021
};
}, [refA, refB]);
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Member

Choose a reason for hiding this comment

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

That's why it shouldn't be variadic.

Copy link
Member Author

Choose a reason for hiding this comment

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

This warning is caused by the inability of the tool to verify if I passed all the dependencies. Since I use all the parameters provided to useForkRef, I know all of them should be in the dependencies (facebook/react#14920 (comment)).

Alternatively, if this really is a no-go for you, would a 4-parameter version of this function be more acceptable (it should cover all cases)?

function useForkRef<Instance>(
  refA: React.Ref<Instance> | null | undefined,
  refB: React.Ref<Instance> | null | undefined,
  refC?: React.Ref<Instance> | null | undefined,
  refD?: React.Ref<Instance> | null | undefined,
)

Copy link
Member

Choose a reason for hiding this comment

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

But spreading is all of the sudden valid e.g. useForkRef(...refs).

Copy link
Member Author

Choose a reason for hiding this comment

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

Why is this not desirable, though?

Copy link
Member

Choose a reason for hiding this comment

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

Because it breaks the rules of hooks

Copy link
Member Author

Choose a reason for hiding this comment

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

I reverted to the original version and added two optional parameters. Please take a look.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think you want to see my point. Abandoning this discussion.

Copy link
Member Author

Choose a reason for hiding this comment

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

Would I ask for it if I didn't want to? For me, the point of code review is to discuss the solution and highlight its good and bad sides.

If you think the solution sucks, I'm ok with that, as long as you explain your reasoning. This way I can either counter your arguments or simply learn something about the existing codebase and patterns.

@mui-pr-bot
Copy link

mui-pr-bot commented Aug 24, 2021

Details of bundle changes (experimental)

Generated by 🚫 dangerJS against 2812477

@github-actions github-actions bot added PR: out-of-date The pull request has merge conflicts and can't be merged and removed PR: out-of-date The pull request has merge conflicts and can't be merged labels Sep 6, 2021
@eps1lon eps1lon removed their request for review September 14, 2021 09:12
@eps1lon eps1lon changed the base branch from next to master September 14, 2021 09:12
@michaldudak michaldudak deleted the variadic-forkRef branch May 12, 2022 14:27
@michaldudak michaldudak restored the variadic-forkRef branch October 5, 2022 08:46
@michaldudak
Copy link
Member Author

Reopening as discussed in #34383

@michaldudak michaldudak reopened this Oct 5, 2022
@@ -0,0 +1,110 @@
import * as React from 'react';
Copy link
Member Author

Choose a reason for hiding this comment

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

This was extracted from mui-material/utils/index.test.js and moved as-is here.

@@ -0,0 +1,38 @@
import * as React from 'react';
Copy link
Member Author

Choose a reason for hiding this comment

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

This was extracted from mui-material/utils/index.test.js and moved as-is here.

@mui-bot
Copy link

mui-bot commented Oct 5, 2022

Details of bundle changes

Generated by 🚫 dangerJS against 5bd1ce1

@@ -0,0 +1,29 @@
import * as React from 'react';
Copy link
Member Author

Choose a reason for hiding this comment

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

This is what remained in mui-material/utils/index.test.js after moving useForkRef and setRef out of this file.

};
}, [refA, refB]);
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain a bit more why it is needed even though the refs is used as dependencies? Is it because eslint can't detect this use case?

The implementation looks good to me.

Copy link
Member Author

@michaldudak michaldudak Oct 6, 2022

Choose a reason for hiding this comment

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

This ESLint rule requires an array literal to be passed in as it can't statically verify the correctness of the code otherwise.

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

👍

@michaldudak michaldudak merged commit 7ce9be6 into mui:master Oct 6, 2022
@michaldudak michaldudak deleted the variadic-forkRef branch October 6, 2022 20:08
alexfauquette pushed a commit to alexfauquette/material-ui that referenced this pull request Oct 14, 2022
daniel-rabe pushed a commit to daniel-rabe/material-ui that referenced this pull request Nov 29, 2022
feliperli pushed a commit to jesrodri/material-ui that referenced this pull request Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Infrastructure work going on behind the scenes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[useForkRef] Accept an array of refs
5 participants