Exploring the Power of TypeScript Generics in React Development

Leul Ayalew
3 min readJun 6, 2023

--

Photo by Lautaro Andreani on Unsplash

Introduction: TypeScript has gained significant popularity among web developers for its ability to provide static typing and improved tooling for JavaScript projects. One of the most powerful features of TypeScript is generics, which allow us to create reusable and flexible code components. In this blog post, we will delve into TypeScript generics and demonstrate how they can be effectively used in React development.

Understanding TypeScript Generics: Generics provide a way to create reusable components and functions that can work with different data types. They allow us to define types that are determined at the time of usage rather than at the time of creation. This flexibility enables us to write more generic code and avoid unnecessary duplication.

Using TypeScript Generics in React: React, being a component-based library, can greatly benefit from the use of generics. Let’s explore some scenarios where TypeScript generics can enhance our React applications:

  1. Creating Reusable Components: Generics can be used to create reusable components that can work with various data types. For instance, if we want to create a generic List component that can render an array of any type, we can define it using generics:
interface ListProps<T> {
items: T[];
}

function List<T>({ items }: ListProps<T>) {
return (
<ul>
{items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
}

// Usage
const numbers: number[] = [1, 2, 3, 4, 5];
const strings: string[] = ["apple", "banana", "cherry"];

<List items={numbers} /> // Renders a list of numbers
<List items={strings} /> // Renders a list of strings

2. Type-safe Props in Higher-Order Components (HOCs): When creating higher-order components (HOCs) that enhance the functionality of existing components, generics can be used to ensure type safety. Consider an example where we want to create a HOC that adds a loading prop to a given component:

function withLoading<TProps>(WrappedComponent: React.ComponentType<TProps>) {
return function WithLoading(props: TProps & { loading: boolean }) {
if (props.loading) {
return <div>Loading...</div>;
}

return <WrappedComponent {...props} />;
};
}

// Usage
interface UserProps {
name: string;
age: number;
}

const UserComponent: React.FC<UserProps> = ({ name, age }) => (
<div>
<p>Name: {name}</p>
<p>Age: {age}</p>
</div>
);

const UserWithLoading = withLoading(UserComponent);

<UserWithLoading name="John" age={25} loading={true} /> // Renders a loading spinner
<UserWithLoading name="John" age={25} loading={false} /> // Renders the UserComponent

3. Generic Hooks: Generics can also be used when creating custom hooks to ensure type safety and reusability. For example, consider a generic useFetch hook that fetches data from an API:

function useFetch<T>(url: string): T | null {
const [data, setData] = useState<T | null>(null);
useEffect(() => {
fetch(url)
.then(response => response.json())
.then(data => setData(data))
.catch(error => console.error(error));
}, [url]);
return data;
}

// Usage
interface User {
name: string;
age: number;
}

const UserComponent: React.FC = () => {
const user: User | null = useFetch<User>("https://api.example.com/user");

if (user === null) {
return <div>Loading...</div>;
}

return (
<div>
<p>Name: {user.name}</p>
<p>Age: {user.age}</p>
</div>
);
}

Conclusion:

TypeScript generics offer immense value in React development, allowing us to write reusable, type-safe, and flexible code components. By leveraging generics, we can create reusable components, enforce type safety in HOCs, and build custom hooks that work with various data types. Incorporating TypeScript generics into your React projects will enhance maintainability and help catch potential errors early in the development process. Embrace the power of generics, and enjoy more robust and scalable React applications!

--

--

Leul Ayalew

I'm a Software Engineer, passionate about building beautiful and performant web and mobile applications.