As React developers, we've all encountered scenarios where we need to render lists of data. While .map() method works well, repeating the same logic every time render a list can become exhausting, and leads to code duplication. Fortunately, there's a cleaner, scalable way to handle this, using reusable component, higher order component, or custom hook.
In this article, I'll share an approach for improving list rendering in React, ensuring your code stays DRY, reusable, and easier to maintain.
Imagine you're building a dashboard for an e-commerce application. The dashboard includes several lists: recent orders, top-selling products, user comments, etc. You need to render each list using a .map() function. Here's a typical example:
const orders = [...]; // Array of order data return ( {orders.map((order, index) => ())} > );
Now, you can see repeating the .map() logic for every list, cluttering your component with similar code. Here’s where reusable pattern can be handy.
To avoid duplicating the .map() logic, we can create a reusable ListComponent that abstracts the mapping logic and allows us to render different components based on the data.
function ListComponent({ data, renderItem }) { return ( {data.map((item, index) => renderItem(item, index))} > ); }
Usage:
( )} />
In this pattern:
renderItem: A function that defines how each item should be rendered
By passing a different renderItem function, we can reuse ListComponent for any list. This results in a clean, reusable component, reducing repetitive .map() logic.
If multiple components need list rendering, let's take this pattern further by creating a HigherOrder Component. A HOC allows to enhance any component with additional functionality — in this case, list rendering.
function withListRendering(WrappedComponent) { return function ListWrapper({ data, ...props }) { return ( {data.map((item, index) => ())} > ); }; }
Usage:
const EnhancedOrderComponent = withListRendering(OrderComponent); // Now render the component with any data array
By wrapping OrderComponent with the withListRendering HOC, we’ve automatically added list rendering behavior without modifying the original component. This pattern keeps code modular.
React hooks offer a functional way to encapsulate logic. If you prefer using hooks, here is an example of list rendering with a custom hook.
function useListRenderer(data, renderItem) { return data.map((item, index) => renderItem(item, index)); }
Usage:
function OrdersDashboard({ orders }) { const orderList = useListRenderer(orders, (order, index) => ()); return {orderList}>; }
This approach moves .map() logic into the hook, keep the rendering logic separate from the component’s structure. It’s another way to keep component lean and focused on presentation.
Let’s apply this pattern to a real-world scenario. Imagine you’re building an e-commerce admin dashboard where multiple lists of orders, products, reviews, etc need to be rendered.
Using the ListComponent approach, you could render a list of orders like this:
( )} />
When we need to render a different list, such as a products, same ListComponent can be reused with different renderItem function:
( )} />
No need to rewrite the .map() logic — just reuse the ListComponent with different data and components. This makes codebase more maintainable as it grows.
The reusable ListComponent pattern simplifies React list rendering by abstracting the repetitive .map() logic. Whether you prefer using the basic component approach, HOC, or custom hook, this pattern ensures code is clean and reusable.
Building a React component with multiple lists, consider using one of these patterns to keep components focused on presentation while separating the list logic outside.
What other reusable patterns have you found useful in React? Let me know in the comments! and finally thanks for reading
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3