コンポーネントツリー内のどこかに配置されたプレースホルダーの反応コンポーネントをレンダリングします。
yarn add react-tunnels
Reactアプリには、一部の要素のコンテンツがchildrenコンポーネントによって定義されるLayoutを定義する一般的なユースケースがあります。たとえば、 Layout一度だけ定義し、すべてのページに再利用する必要がありますが、その手順がchildrenコンポーネントに依存するブレッドクラブがあります。この小さなライブラリを使用すると、トンネルを定義して、要素からアプリ内の別の要素、さらにはツリーの上にある要素にレンダリングできます。 Portalのようなものですが、ターゲットはDOM要素の代わりにコンポーネントです。
idで識別されたTunnelPlaceholderを定義し、アプリ内のどこにでも同じIDを持つTunnelコンポーネントを定義することにより、 render関数に渡されるプロパティを決定します。単一のTunnelのみを定義する場合、その小道具はrender関数に直接渡されます。単一のidに複数のTunnelがある場合、プレースホルダーrender関数は、各Tunnelのpropsを含む配列であるitem引数を受け取ります。いくつかの例を見てみましょう。
レンダリング関数のないプレースホルダーを定義して、同じIDを持つTunnelコンポーネントから来る子供をレンダリングします。
import { TunnelProvider , TunnelPlaceholder , Tunnel } from 'react-tunnels'
render (
< TunnelProvider >
< div >
< TunnelPlaceholder id = "my-tunnel" />
< Tunnel id = "my-tunnel" >
This will be rendered on the placeholder ?
</ Tunnel >
</ div >
</ TunnelProvider >
)ここで実際の例を確認してください
TunnelPlaceholderのProp multipleを使用してパンムを構築するのは簡単です。これにより、複数のトンネルがあることを知らせることができ、 render関数は一連の小道具で呼び出されます。
const Breadcrumbs = ( ) => (
< TunnelPlaceholder id = "breadcrumb" multiple >
{ ( { items } ) => (
items . map ( ( { children , href } ) => (
< span > < a href = { href } > { children } </ a > </ span >
) )
) }
</ TunnelPlaceholder >
)
const Breadcrumb = ( { children , url } ) => (
< Tunnel id = "breadcrumb" href = { url } >
{ children }
</ Tunnel >
)
render (
< TunnelProvider >
{ /* This will render the breadcrumb */ }
< Breadcrumbs />
{ /* Somewhere else in children */ }
< Breadcrumb url = "/products" > Products </ Breadcrumb >
< Breadcrumb url = "/products/123" > Product < strong > 123 </ strong > </ Breadcrumb >
</ TunnelProvider >
)ここでライブの例を確認してください
このプロジェクトは、Javi Velascoによって開発され、さまざまなReactプロジェクトのパン塊コンポーネントとLayoutカスタマイズを構築する方法として開発されました。どんなフィーバック、ヘルプ、または改善が高く評価されています。
このプロジェクトは、MITライセンスの条件に基づいてライセンスされています。