-
Notifications
You must be signed in to change notification settings - Fork 96
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
we cant use optional refs in useMergeRef #190
Comments
I create a pull request but I set a bad commit message for that so it doesn't link to this issue |
Why should you be able to do this? You can't conditionally use hooks. |
Please note at my pull request that nothing special has changed. I just use a filter for empty refs. |
That's not all it does |
@47vigen Was there some use case that you were trying to solve with this? The code in its current form is exactly the same as what you have provided, except yours is more verbose and allows for Breaking it down:
The addition of
This is unnecessary since the code already checks for null and undefined values:
The condition If your use case is that you want to account for undefined values, you can simply update the call of const C = React.forwardRef(function C(
props: {
innerRef?: React.Ref<HTMLDivElement>;
},
ref: React.ForwardedRef<HTMLDivElement>
) {
const myRef = React.useRef<HTMLDivElement>();
const merged = useMergedRef(ref, myRef, props.innerRef ?? null);
return <div ref={merged}>Hello world.</div>;
}); |
I think it's better we can use optional ref, sometimes you want to get some optional ref from props so you cant use the current useMergeRef for this!
so change the refs argument to optional and use a filter before "for".
The text was updated successfully, but these errors were encountered: