-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Generic Component with forwarded ref. #106
Comments
TypeScript's inability to retain free type parameters when doing higher order functional programming is unfortunate, this is really the best way to do it if you want to use One workaround I use is to just not use type ListProps<ItemType> = {
items: ItemType[]
innerRef?: React.Ref<{ scrollToItem(item: ItemType): void }>
}
function List<ItemType>(props: ListProps<ItemType>) {
useImperativeHandle(props.innerRef, () => ({
scrollToItem() { }
}))
return null
} |
-- https://reactjs.org/docs/hooks-reference.html#useimperativehandle This is more of a recommendation so that you don't leak more implementation detail than necessary. Depending on the use case it might make more sense to use the default |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions! |
Hello everyone,
today I stumbled across the following problem:
I want to build a List Component that has a generic prop . From the outside you can pass a ref to the List, to access a scrollToItem(item: TItem). Since I want to use hooks, I need to use React.forwardRef, which does not allow to return a generic function.
This is my attempt of solving, but I feel like there could be something less verbose and repetitive.
The text was updated successfully, but these errors were encountered: