Guide to Using ComponentProps in React with TypeScript and Custom Props
NodeJSReact
A
admin1·19 sep, 2024·8 min read
All Articles

Using ComponentProps in React with TypeScript and Custom Prop


When working with components in React and TypeScript, one of the biggest benefits is the ability to have strong typing. ComponentProps is a utility in TypeScript that allows us to infer the props of a component and reuse them in other contexts, which improves code consistency and maintainability.

What is ComponentProps?


ComponentProps is a generic type that extracts the properties (props) of a React component. Instead of manually defining the props in each component or duplicating types, you can use this utility to automatically derive the props type from an existing component.
The basic syntax is:

JavaScript
import { ComponentProps } from 'react';
type ButtonProps = ComponentProps<'button'>;

In this case, ButtonProps will include all the standard properties that a native HTML <button> element accepts, such as onClick, disabled, etc.

Using ComponentProps with Custom Components

If you're creating a custom component in React, ComponentProps can be especially useful when you want to extend or modify the props.

JavaScript
import React from 'react';

type CustomButtonProps = ComponentProps<'button'> & {
  customProp: string;
};

const CustomButton: React.FC<CustomButtonProps> = ({ customProp, ...rest }) => {
  return (
    <button {...rest}>
      {customProp}
    </button>
  );
};

Here, CustomButton has all the properties of a native <button> element, and we've also added a custom customProp.

Customizing Material-UI (MUI)

Here’s a simple example using Material-UI (MUI) with ComponentProps for a customized button
JavaScript
import React from 'react';
import Button from '@mui/material/Button';
import { ComponentProps } from 'react';

// Extract props from MUI Button component
type CustomButtonProps = ComponentProps<typeof Button> & {
  customLabel: string;
};

const CustomButton: React.FC<CustomButtonProps> = ({ customLabel, ...props }) => {
  return (
    <Button {...props}>
      {customLabel}
    </Button>
  );
};

// Component usage
const App = () => {
  return (
    <CustomButton customLabel="Click Me" variant="contained" color="primary" />
  );
};

export default App;

In this example, CustomButton reuses all the props that the MUI Button component accepts and adds a custom customLabel prop. You can continue using MUI props like variant, color, and more.

Making a Box with Native div Element

You can also use ComponentProps to customize native HTML elements, like a div, or extend a component from libraries like Material-UI. Here's an example where we customize a Box component using the props from a native div element:

JavaScript
import React from 'react';
import { ComponentProps } from 'react';

// Extract props from native div element
type BoxProps = ComponentProps<'div'> & {
  backgroundColor: string;
};

const Box: React.FC<BoxProps> = ({ backgroundColor, children, ...rest }) => {
  return (
    <div style={{ backgroundColor }} {...rest}>
      {children}
    </div>
  );
};

// Component usage
const App = () => {
  return (
    <Box backgroundColor="lightblue" className="custom-class">
      This is a custom box!
    </Box>
  );
};

export default App;

In this example, Box extends all the default properties of a div element (such as className, onClick, etc.), and we add a custom backgroundColor property for extra flexibility


Benefits of Using ComponentProps

  • Type Reuse: You avoid type duplication when several components share the same props.
  • Consistency: By deriving props from existing components, you ensure that all components stay synchronized in terms of their types.
  • Extensibility: You can easily extend the props of native or custom components.
2.4k

Topics in this article

Follow topics you love
NodeJSReact

Discussion

3 comments

Share your thoughts...

Este sitio utiliza cookies para mejorar tu experiencia. Al continuar navegando, aceptas su uso. Puedes deshabilitarlas desde tu navegador.