I like to follow some guidelines when writing React components.
export type ButtonProps = {
disabled: boolean
variant?: 'primary' | 'secondary',
}
export function Button({ disabled, variant}: ButtonProps): ReactElement {
// implementation
}
Props
- Naming props with the component name and the suffix props
- Define the props above the component
- Define each prop in the same type, then can be later used as "ButtonProps['variant']", unless they are objects.
Component
- Named export
- Define the props explicitly, avoid "function Button({ props })"
- Return a ReactElement