DEV Community

Cover image for How to Create a Reusable React + TypeScript Component (Simple and Clean)
Gunnar Halen
Gunnar Halen

Posted on • Edited on

1 1

How to Create a Reusable React + TypeScript Component (Simple and Clean)

One of the most common tasks in a React project is creating reusable components that are flexible and easy to maintain.

In this post, I’ll share a quick and practical pattern to build typed, reusable components in React + TypeScript — keeping it simple and effective for real-world use.


✅ The Problem: Reusability + Type Safety

Imagine you need a button component that supports different variants (like primary, secondary), handles clicks, and accepts any content inside it. How would you type it properly?

Here’s a clean way and efficient way to do it:


💻 The Solution: Union Types + React Props

type ButtonProps = {
  variant?: 'primary' | 'secondary';
  onClick: () => void;
  children: React.ReactNode;
};

export const Button = ({ variant = 'primary', onClick, children }: ButtonProps) => {
  return (
    <button className={`btn ${variant}`} onClick={onClick}>
      {children}
    </button>
  );
};
Enter fullscreen mode Exit fullscreen mode

⚙️ Why this works great?

  • variant is defined as a union type ('primary' | 'secondary'), making them type-safe and auto-completed by your editor.
  • children allows flexibility to pass any content (like icons or text).
  • Default value for variant simplifies usage.
  • Easy to extend — just add more variants as needed.

🔑 Usage Example:

<Button onClick={() => console.log('Clicked!')}>
  Click me
</Button>

<Button variant="secondary" onClick={() => alert('Secondary clicked')}>
  Secondary Action
</Button>
Enter fullscreen mode Exit fullscreen mode

💡 Pro Tip:

Want to add styled-components or CSS Modules? You can map variant directly to class names or styled variants like this:

<Button variant="secondary" onClick={() => alert('Secondary Clicked!')}>
  Secondary Action
</Button>
Enter fullscreen mode Exit fullscreen mode
const StyledButton = styled.button<{ variant: 'primary' | 'secondary' }>`
  background: ${({ variant }) => (variant === 'primary' ? 'blue' : 'gray')};
  color: white;
  padding: 10px 20px;
`;
Enter fullscreen mode Exit fullscreen mode

This is a simple pattern to keep React components clean, flexible, and maintainable — something that can help any Front-End Developer in daily work.

👉 How do you handle reusable components in your projects? Share your approach in the comments! 👇

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple “thank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay