-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add immutable forwardRef
typing alternative
#323
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
Conversation
@@ -28,6 +28,20 @@ export const FancyButton = React.forwardRef<Ref, Props>((props, ref) => ( | |||
)); | |||
``` | |||
|
|||
`forwardRef` alternative without mutability: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i dont understand what "without mutability" means. do you mind elaborating in here? how does this materially differ from the example above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
React.forwardRef<HTMLElement, Props>
assigns the type ((instance: T | null) => void) | MutableRefObject<T | null> | null
to the ref
parameter. It's a MutableRefObject<T | null>
, unlike with React.Ref<T>
. The latter returns ((instance: T | null) => void) | RefObject<T> | null
, which seems to be an improvement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which seems to be an improvement.
We should explain this in the cheatsheet where this would be an improvement. It would always be an improvement then you should propose the change to @types/react
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the discussion!
It appears that an opposing issue was raised and a PR has been merged to make the ref
parameter of forwardRef
mutable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow. we both learned something today...
No description provided.