Skip to content

Add default value for useRef calls in .tsx files#348

Merged
taylorotwell merged 1 commit intolaravel:1.xfrom
matthiasweiss:add_default_value_useref
Jan 15, 2024
Merged

Add default value for useRef calls in .tsx files#348
taylorotwell merged 1 commit intolaravel:1.xfrom
matthiasweiss:add_default_value_useref

Conversation

@matthiasweiss
Copy link
Copy Markdown
Contributor

@matthiasweiss matthiasweiss commented Jan 14, 2024

This fixes missing default values for useRef calls that use generics, in this case HTMLInputElement.

Currently the returned value is inferred as HTMLInputElement | null | undefined. Adding null as a default value is better, since the returned value is inferred as HTMLInputElement | null due to the signature function useRef<T>(initialValue: T | null): RefObject<T>;.

I ran into this issue because I swapped the <TextInput /> in the <UpdatePasswordForm /> component with a different input component (https://ui.shadcn.com/docs/components/input), which caused an error when passing the ref down to the new component. Adding null as the default value fixed that issue.

Copy link
Copy Markdown
Member

@jessarcher jessarcher left a comment

Choose a reason for hiding this comment

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

Looks good to me. The React docs demonstrate passing an initial value of null in this scenario too.

https://react.dev/reference/react/useRef#manipulating-the-dom-with-a-ref

@taylorotwell taylorotwell merged commit e853918 into laravel:1.x Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants