百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-tunnels

> 前端框架
开源

在组件树中其他位置放置的占位符中渲染 React 组件。

395 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

在组件树中其他位置放置的占位符中渲染 React 组件。

React Tunnels

Render React components in placeholders that are placed somewhere else in the component tree.

Install

bash
yarn add react-tunnels

Why

There is a common use case in React apps where you want to define a Layout where the content of some elements is defined by children components. For example, you want to define Layout just once and reuse it for every page but it has a breadcrumb whose steps depend on children components. This tiny library allows you to define tunnels to render from an element to whatever another element in the App, even elements located on top of the tree. It's like Portal but the target is a component instead of a DOM element.

Usage

Define a TunnelPlaceholder identified by an id and decide what properties are going to be passed to its render function by defining Tunnel components with the same id anywhere else in the app. If you define just a single Tunnel its props will be passed straight to the render function, if there is more than one Tunnel for a single id, the placeholder render function will receive an item argument which is an Array containing the props for each Tunnel. Let's see some examples.

Simple example: children tunneling

Define a placeholder without any render function so it will render any children coming from a Tunnel component with the same id.

javascript
import { TunnelProvider, TunnelPlaceholder, Tunnel } from 'react-tunnels'

render(
  
    

  
)

Check the real example here

More complex example: building a Breadcrumb

It's easy to build a breadcrumb using the prop multiple in the TunnelPlaceholder. This allows to let it know that there will be multiple tunnels so the render function will be called with an array of props.

javascript
const Breadcrumbs = () => (
  
    {({ items }) => (
      items.map(({ children, href }) => (
        {children}
      ))
    )}
  
)

const Breadcrumb = ({ children, url }) => (
  
    {children}
  
)

render(
  
    {/* This will render the breadcrumb */}
    
    {/* Somewhere else in children */}
    Products
    Product 123
  
)

Check the live example here

Similar Libraries

  • React Slot Fill: A similar project built by Cameron Westland with a slightly different API and a bit more limited use cases. The main difference is that you can't pass content to a placeholder from multiple entry points. react-tunnels does this by passing an array with the props defined by each tunnel to the render function of the placeholder. For simple cases though, it is pretty similar.
  • Preact Slots: A library similar to React Slot Fill but for Preact developed by Jason Miller.

About

This project has been developed by Javi Velasco as a way to build Breadcrumb components and Layout customizations for a variety of React projects. Any feeback, help or improvements is highly appreciated.

License

This project is licensed under the terms of the MIT license.

Issues· 21 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptbreadcrumbscomponentsinverse-renderingreact

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架