Last active
July 15, 2023 21:00
-
-
Save Stringsaeed/9298caf9155ec535acd21d570a8750fc to your computer and use it in GitHub Desktop.
Render Item Hook to use in flatlists in react-native
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useCallback } from "react"; | |
import { ListRenderItem } from "react-native"; | |
type BaseProps<T extends unknown> = { | |
item: T; | |
}; | |
type UseRenderItemProps<T, P extends BaseProps<T>> = { | |
Component: React.ComponentType<P>; | |
componentProps: Omit<P, "item">; | |
}; | |
const useRenderItem = <T, P extends BaseProps<T>>({ | |
componentProps, | |
Component, | |
}: UseRenderItemProps<T, P>) => { | |
const renderItem: ListRenderItem<T> = useCallback( | |
({ item }) => <Component {...(componentProps as P)} item={item} />, | |
[Component, componentProps] | |
); | |
return renderItem; | |
}; | |
export default useRenderItem; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment