// Composite Component patterns: // - children slot for free-form composition // - render-prop slot when the server must pass data into client UI // - component prop slot for reusable typed interactive controls import type { ComponentType, ReactNode } from 'react' import { createFileRoute } from '@tanstack/react-router' import { createServerFn } from '@tanstack/react-start' import { CompositeComponent, createCompositeComponent, } from '@tanstack/react-start/rsc' import { z } from 'zod' // Replace with your own server-only data layer declare const db: { posts: { findById(postId: string): Promise<{ id: string authorId: string title: string body: string }> } products: { findById(productId: string): Promise<{ id: string name: string price: number }> } } const getPostCard = createServerFn({ method: 'GET' }) .inputValidator(z.object({ postId: z.string() })) .handler(async ({ data }) => { const post = await db.posts.findById(data.postId) const src = await createCompositeComponent<{ children?: ReactNode renderActions?: (args: { postId: string; authorId: string }) => ReactNode }>((props) => (

{post.title}

{post.body}

{props.children}
)) return { src } }) export const Route = createFileRoute('/posts/$postId')({ loader: async ({ params }) => getPostCard({ data: { postId: params.postId } }), component: PostPage, }) function PostPage() { const { src } = Route.useLoaderData() return ( ( )} > ) } function Comments() { return
Interactive comments go here
} function PostActions(props: { postId: string; authorId: string }) { return ( ) } // Component-prop slot variant const getProductCard = createServerFn({ method: 'GET' }) .inputValidator(z.object({ productId: z.string() })) .handler(async ({ data }) => { const product = await db.products.findById(data.productId) const src = await createCompositeComponent<{ AddToCart?: ComponentType<{ productId: string; price: number }> }>((props) => (

{product.name}

{props.AddToCart ? ( ) : null}
)) return { src } }) function AddToCartButton(props: { productId: string; price: number }) { return ( ) } // Example usage somewhere else: // const { src } = await getProductCard({ data: { productId: 'p-1' } }) //