Last active
November 22, 2023 20:29
-
-
Save MR-Mostafa/6bec49dbc1d26e791cf2044260563480 to your computer and use it in GitHub Desktop.
Build strongly typed polymorphic components with React and TypeScript - Part 2
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 * as React from 'react'; | |
/** | |
* یک جنریک تایپ است که بیانگر React.ElementType تایپ | |
* 1. میباشد HTML تمامی تگهای ولید و معتبر | |
* 2. میتواند یک کامپوننت ریاکتی باشد | |
*/ | |
type ButtonProps<C extends React.ElementType> = { | |
as?: C; | |
children: React.ReactNode; | |
}; | |
export const Button = <C extends React.ElementType>({ | |
as, | |
children, | |
...otherProps | |
}: ButtonProps<C>) => { | |
/** | |
* as در صورت عدم تعیین پراپرتی DOM مقدار پیشفرض المنت رندرشده در | |
*/ | |
const Component = as || 'button'; | |
return <Component {...otherProps}>{children}</Component>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment