react-portalslots

react-portalslots

react-portalslots: colocate UI components without prop drilling

Building React apps often involves a common problem: you need to render parts of your UI (buttons, toolbars, headers, etc.) into specific areas of your layout (header, sidebar, footer), without drilling props all the way up through the component tree or relying on global state.

The react-portalslots library solves this elegantly.
It lets you “teleport” UI fragments from deep inside your component tree into named layout slots – powered by React Portals and Context – with zero global state and minimal boilerplate.

The Idea Behind react-portalslots

The Problem: Prop Drilling & “Upward Passing”

When you have a Layout component that defines regions like a header or sidebar, nested components often want to add something there (like a “Save” button in the header).

You usually end up doing something like:

<Layout
  header={<button>Savebutton>}
  sidebar={<div>Navigationdiv>}
>
  <Toolbar />
Layout>

This quickly becomes messy – you have to “lift” components up through layers of unrelated components. It breaks locality and composability.

The Solution: Named Portals + Slots

react-portalslots introduces the concept of named slots – pre-defined areas of your layout that can receive content from anywhere in the React tree.

You define “slots” in your layout:

<HeaderSlot.Slot />
<SidebarSlot.Slot />
<FooterSlot.Slot />

And later, deep inside your components, just render:

<HeaderSlot>
  <button>Savebutton>
HeaderSlot>

The library automatically “registers” this content and renders it inside the corresponding slot.
This removes the need for prop-drilling, context stores, or global state management.

API Overview

PortalSlotsProvider

Wrap your app (or the section that uses slots) with the provider:

<PortalSlotsProvider>
  <App />
PortalSlotsProvider>

This creates a registry that tracks all active slots and portal contents.

PortalSlot(name?: string)

Create a named slot with a simple factory call:

const HeaderSlot = PortalSlot('header');
const FooterSlot = PortalSlot('footer');

Each slot provides two components:

  • – the portal component used anywhere in the tree.
  • – the container that marks where content should appear.

Here’s a minimal working example:

const HeaderSlot = PortalSlot('header');
const FooterSlot = PortalSlot('footer');

function Layout({ children }) {
  return (
    <div>
      <header>
        <HeaderSlot.Slot />
      header>
      <main>{children}main>
      <footer>
        <FooterSlot.Slot />
      footer>
    div>
  );
}

function App() {
  return (
    <PortalSlotsProvider>
      <Layout>
        <HeaderSlot>
          <button>Savebutton>
        HeaderSlot>

        <FooterSlot>
          <small>© 2025small>
        FooterSlot>

        <div>Page content…div>
      Layout>
    PortalSlotsProvider>
  );
}

Result: the

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
brand-you:-your-formula-for-a-powerful-personal-brand

Brand you: Your formula for a powerful personal brand

Next Post
improving-binary-security-in-mobile-application:-a-deep-dive-into-obfuscation

Improving Binary Security in Mobile Application: A Deep Dive into Obfuscation

Related Posts