You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently React refs do not work when you applying it to different elements depending on some condition. I'd like to not have Typescript errors for this case
Use Cases
Sometimes we need to conditionally render different elements and attach refs to them:
functionButton({ href }: {href: string}){constref=React.useRef<HTMLAnchorElement|HTMLButtonElement>()if(href){return<aref={ref}></a>// Type 'RefObject<HTMLAnchorElement | HTMLButtonElement>' is not assignable// to type 'RefObject<HTMLAnchorElement>'.}return<buttonref={ref}></button>}
This code is valid Javascript, but reports an error in Typescript.
I think, this is also related to the general covariance issue: #10717 (comment)
But I am wondering if there could be a more simple solution to this particular case
The text was updated successfully, but these errors were encountered:
Search Terms
react ref union type
Suggestion
Currently React refs do not work when you applying it to different elements depending on some condition. I'd like to not have Typescript errors for this case
Use Cases
Sometimes we need to conditionally render different elements and attach refs to them:
This code is valid Javascript, but reports an error in Typescript.
Playground example
I think, this is also related to the general covariance issue: #10717 (comment)
But I am wondering if there could be a more simple solution to this particular case
The text was updated successfully, but these errors were encountered: