Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions path/to/ForwardedInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React, { forwardRef, Ref, InputHTMLAttributes } from 'react';

type ForwardedInputProps = InputHTMLAttributes<HTMLInputElement>;

const ForwardedInput = forwardRef<HTMLInputElement, ForwardedInputProps>(
(props, ref: Ref<HTMLInputElement>) => {
return <input ref={ref} {...props} />;
}
Comment on lines +6 to +8
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The explicit type annotation ref: Ref<HTMLInputElement> is redundant and not entirely correct. The ref parameter's type is ForwardedRef<HTMLInputElement>, which is subtly different from Ref. It's best practice to let TypeScript infer this type from forwardRef. This also allows for a more concise implementation.

After applying this change, you can remove the Ref import from line 1 as it will no longer be used.

  (props, ref) => <input ref={ref} {...props} />

);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

To improve debuggability with React DevTools, it's a good practice to add a displayName to components created with forwardRef.

);

ForwardedInput.displayName = 'ForwardedInput';


export default ForwardedInput;