HOME
SERVICES
WORKABOUT US

Jun 13, 2024

Component Reusability

Web Development

Front-End Development

Performance

Component Reusability Principles — Part II
Cover for Component Reusability Principles — Part II
Image source: Storyset

Introduction

Welcome back to our series on mastering component reusability in React! In our first episode, we covered foundational principles. These include the Single Responsibility Principle, Prop-Driven Design, and keeping Props APIs simple and intuitive.

Now, let's dive deeper into how you can optimize the performance of your components.

Optimizing for Performance

Performance optimization is crucial for maintaining smooth and functional applications. Below, we’ll explore common pitfalls. We'll also look at their more efficient alternatives in React component design.

Memoization 🧬

Memoization in React can prevent unnecessary re-renders. It does this by caching and reusing the results of costly function calls or rendering. It does this when the inputs are the same.

❌ Poor Practice: Unnecessary Re-renders in a List

A common issue arises when components within a list re-render unnecessarily due to parent component updates, even when the data they display does not change.

function UserList({ users }) {
  return (
    <ul>
      {users.map(user => <UserItem key={user.id} user={user} />)}
    </ul>
  );
}

function UserItem({ user }) {
  console.log("Rendering user:", user.name); // Logs every time the parent renders
  return <li>{user.name}</li>;
}

✅  Best Practice: Unnecessary Re-renders in a List

Applying React.memo to UserItem ensures that it only re-renders when its props change. It does not re-render every time its parent does.

const UserItem = React.memo(function UserItem({ user }) {
  console.log("Rendering user:", user.name); // Logs only if `user` prop changes
  return <li>{user.name}</li>;
});

Balancing the Use of Memoization

React.memo and other memoization techniques can improve performance by preventing unnecessary re-renders. But, they are not always the solution. Each use of memoization introduces an overhead of comparing props, and in cases where props change frequently or the component logic is trivial, this can lead to worse performance. Analyze component performance and weigh benefits against costs before applying memoization.

As a rule, consider memoizing when:

  • Components that are expensive to render.

  • Components that render frequently with the same props.

  • Components deep in the component tree that do not change state often.

This balanced approach will help you use memoization well. It will keep you from overusing it. This will maintain the best application performance and resource usage.

Lazy Loading ⏳

Lazy loading delays the start. It also delays the rendering of components that are not immediately needed. This speeds up the app's initial load time.

❌ Poor Practice: Immediate Loading Heavy Components

Loading resource-heavy components upfront can greatly delay your app's interactivity. This is especially true if those components are not immediately needed.

import DetailedMap from './DetailedMap';

function Dashboard() {
  return (
    <div>
      <h1>Dashboard</h1>
      <DetailedMap />  // Loads a complex component even if not immediately viewed
    </div>
  );
}

✅  Best Practice: Using React.lazy

React.lazy helps you load components only when they are needed, improving initial load times. This is especially useful for components that aren't immediately visible. This includes components accessed via routing.

const DetailedMap = React.lazy(() => import('./DetailedMap'));

function Dashboard() {
  return (
    <div>
      <h1>Dashboard</h1>
      <React.Suspense fallback={<div>Loading Map...</div>}>
        <DetailedMap />
      </React.Suspense>
    </div>
  );
}

Finding the Right Balance with Lazy Loading

Lazy loading is a powerful tool for improving web app performance. It splits the code at logical points and only loads pieces as needed. However, it’s crucial not to overuse this technique.

When to Use Lazy Loading 🟢

  • Large Components: Components that are large and not needed right away should be candidates for lazy loading. This typically includes secondary views, large visualizations, and dialogs that aren't shown until user interaction.

  • Route-based Code Splitting: This is one of the most common and effective uses of lazy loading. Components rendered in different routes can be loaded only when those routes are visited.

When to Avoid Lazy Loading 🔴

  • Small Components: If the component is relatively small, the overhead of lazy loading might outweigh the benefits. The added complexity of managing loading states and potential rendering delays might hurt user experience.

  • Critical Path Components: Components that are essential for the initial user interaction should not be lazy loaded. Delaying their availability can make the app feel slower to the user.

Using lazy loading strategically will ensure that your application remains responsive and efficient, without sacrificing the user experience on critical interactions. Always profile your app to find the best candidates for lazy loading. Avoid it where it adds unnecessary complexity or delays.

Efficient Handling of Lists ⚡

Handling large lists efficiently is crucial in React to prevent performance bottlenecks caused by rendering unnecessary or off-screen elements, which can drastically slow down the user experience.

❌ Poor Practice: Rendering Large Lists Without Optimization

Rendering large lists without any optimization can lead to performance bottlenecks, especially if the list grows dynamically.

function List({ items }) {
  return (
    <ul>
      {items.map(item => <li key={item.id}>{item.name}</li>)}
    </ul>
  );
}

✅  Best Practice: Using Virtualization

Implementing virtualization only renders items in the list that are currently visible to the user, greatly improving the performance of large lists.

import { FixedSizeList as List } from 'react-window';

function MyList({ items }) {
  return (
    <List
      height={400}
      width={300}
      itemCount={items.length}
      itemSize={35}
    >
      {({ index, style }) => <div style={style}>{items[index].name}</div>}
    </List>
  );
}

Conclusion

In this episode of our series, we've taken a deeper dive into optimizing the performance of your components. Through strategies such as memoization, lazy loading, and efficient list handling, we've explored how to make your React applications not only more efficient but also better from a user experience.

Stay tuned for more insights in our next post, where we will explore additional techniques to further refine your React components. As always, happy coding, and may your applications run efficiently and your development journey be rewarding! 🪴


If you enjoyed this post, you might also like:

Torii Studio

Company

Our Story

Our Values

Our Approach

New York City

8 Spruce St,

41st Floor, SUITE F

New York, NY, 10038

Copyright © 2020-2024 Torii Studio, Inc.