One React Component, Two Layouts: A Pattern for Better UI Architecture
My portfolio has two completely different layouts that share one component and one data file. Here's the dual view pattern that makes it possible....
This article discusses a design pattern that allows a single React component to handle two distinct UI layouts - a grid of cards that open into modals, and a full-screen vertical carousel with snap scrolling. The key to this pattern is sharing a single data source and one component that adapts its rendering based on a single boolean prop indicating which layout to use.
At the heart of the pattern is a flat array of project objects in a file called projects.js. Each project object contains an id, title, tech stack, and a concise description. This single data layer is imported directly by both the grid and carousel views, giving them access to identical project data. The id field serves multiple purposes - it's the GitHub repo slug, image filename key, and the list key for mapping projects in each view.
The core reusable component is ProjectItem, which takes a project object and a boolean isGrid prop. When isGrid is true, the component renders a compact card with title, description, and code link, optimized for scanning. When isGrid is false, it renders the full project details in a 12-column layout perfect for reading. The component's rendering changes based solely on this one prop, even though both layouts display the same project data.
The grid view uses a CSS grid system with clickable cards that open a modal for full project details. The carousel view wraps the same ProjectItem component in a vertical scroll-snap carousel, with each slide representing the full project view. No additional modal UI is needed for the carousel.
To personalize the user's preferred layout, the pattern includes a ViewToggle component that persists the chosen view (grid or list) in the browser's localStorage between sessions. This allows the user to switch between the two layouts without losing their preference.
The pattern shines when the same data needs to be displayed in different densities - for scanning in the grid view versus full reading in the carousel view. It also works well when the same data serves different interaction models - browsing in the grid vs. deep-dive in the carousel. The two layouts should feel fundamentally different with complementary strengths, rather than redundant.
However, the pattern breaks down when the branching logic becomes deeply nested, or when the data requirements of each layout diverge significantly. In these cases, splitting the component into two separate components might be more appropriate. The article concludes that one component, two layouts works best when the branching is shallow and the data is identical across both views.
Rewritten by urgent.news; may contain errors — check the original.
Source: hackernoon.com
Read the original at hackernoon.com →
More from HackerNoon on urgent.news
Report a problem with this story
Get the day's news by email. Daily, weekly or monthly. Sign up