May 30, 2024•4 min read••
Tags ▼
- react
- layout components
- javascript
![]()
Layout components in React are specialized components designed to arrange other components on a page. Their primary role is to manage the layout, allowing the main content components to remain agnostic about their placement. This separation of concerns enhances flexibility and reusability. Examples include split screens, lists, and modals.
A split-screen component divides the screen into two sections, each displaying a different component. Here's how you can create one:
SplitScreen.js, and define the component with left and right props.import styled from 'styled-components'; export const SplitScreen = ({ left: Left, right: Right }) => { return ( <Container> <Pane><Left /></Pane> <Pane><Right /></Pane> </Container> ); }; const Container = styled.div` display: flex; `; const Pane = styled.div` flex: 1; `;
To make the split-screen component more flexible, you can add weight props to control the space each side occupies:
leftWeight and rightWeight props with default values.export const SplitScreen = ({ left: Left, right: Right, leftWeight = 1, rightWeight = 1 }) => { return ( <Container> <Pane weight={leftWeight}><Left /></Pane> <Pane weight={rightWeight}><Right /></Pane> </Container> ); }; const Pane = styled.div` flex: ${props => props.weight}; `;
Lists are another common layout pattern. You can create reusable list components that display different types of items:
export const RegularList = ({ items, resourceName, itemComponent: ItemComponent }) => { return ( <> {items.map((item, i) => ( <ItemComponent key={i} {...{ [resourceName]: item }} /> ))} </> ); };
To create a numbered list, extend the regular list component to include item numbers:
export const NumberedList = ({ items, resourceName, itemComponent: ItemComponent }) => { return ( <> {items.map((item, i) => ( <React.Fragment key={i}> <h3>{i + 1}</h3> <ItemComponent {...{ [resourceName]: item }} /> </React.Fragment> ))} </> ); };
Modals are used to display content over the main page. Here's how to create a simple modal component:
Modal.js, and define the modal component with state management for visibility.import React, { useState } from 'react'; import styled from 'styled-components'; export const Modal = ({ children }) => { const [shouldShow, setShouldShow] = useState(false); return ( <> <button onClick={() => setShouldShow(true)}>Show Modal</button> {shouldShow && ( <ModalBackground onClick={() => setShouldShow(false)}> <ModalBody onClick={e => e.stopPropagation()}> {children} <button onClick={() => setShouldShow(false)}>Hide Modal</button> </ModalBody> </ModalBackground> )} </> ); }; const ModalBackground = styled.div` position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); `; const ModalBody = styled.div` position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: white; padding: 20px; `;
Layout components in React help manage the arrangement of other components on a page. By separating layout concerns from content concerns, you gain flexibility and reusability in your codebase. Whether you're creating split screens, lists, or modals, understanding and utilizing layout components can significantly enhance your development workflow.
You can read this article in Medium
Follow on your preferred channel for new articles, notes, and experiments.