Skip to content

Instantly share code, notes, and snippets.

@MR-Mostafa
Last active November 22, 2023 20:29
Show Gist options
  • Save MR-Mostafa/6bec49dbc1d26e791cf2044260563480 to your computer and use it in GitHub Desktop.
Save MR-Mostafa/6bec49dbc1d26e791cf2044260563480 to your computer and use it in GitHub Desktop.
Build strongly typed polymorphic components with React and TypeScript - Part 2
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