Course contentsShow
← React Native
Lesson 160 of 1,5553. Core Primitive ComponentsFree lesson

Building Resilient Spacer Components

Learners will create reusable structural spacing primitives to maintain consistent design tokens.

In this lesson, you will learn how to create a reusable Spacer component to enforce consistent design gaps across your application without cluttering your code with repetitive margin styles.

The Problem with Manual Margins

When you rely on adding marginBottom or marginRight directly to individual components, your layout quickly becomes brittle. If your design team decides to change the standard "gap" between items from 16px to 20px, you would have to manually update every single component in your codebase. This manual approach is error-prone and makes your code harder to read.

The Resilient Spacer Pattern

A Spacer component is a simple, structural primitive that does one thing: it occupies space. By creating a dedicated component, you move the spacing logic into a single "Source of Truth."

Consider this simple implementation:

import React from 'react';
import { View } from 'react-native';

const Spacer = ({ size = 16 }) => {
  return <View style={{ height: size, width: size }} />;
};

export default Spacer;

Why This is Resilient

By abstracting the View into a Spacer, you achieve three things:

  1. Consistency: All developers on your team use the same component to create gaps, ensuring design token uniformity.
  2. Readability: Your JSX becomes cleaner. Instead of guessing why a View has a margin-top: 24, you see a <Spacer size={24} />, which explicitly documents its intent.
  3. Maintainability: If you need to add logic later—such as ensuring a spacer doesn't render if it's the last item in a list—you only have to update the Spacer component code in one place, and the entire app benefits instantly.

Using a Spacer allows your layout to remain flexible, as you can now adjust spacing globally by simply modifying your component’s default props or logic. This primitive approach effectively separates your layout content from your layout structure.

Key Takeaway: Build resilient layouts by abstracting empty spacing into a dedicated, reusable component to ensure consistency and simplify global design updates.