Skip to content

feat: Expose client contexts #6664

Open

Description

@mrmattrc

Describe the feature you'd like to request

When using R3F and (specifically Drei's Html component), we need to have access to the client context so that we can pass it to the useContextBridge hook. Without access to the client contexts, it seems impossible to use our tRPC hooks inside components nested under the Html component.

Describe the solution you'd like to see

Currently, the React client contexts are in the src/internals directory (see Context.tsx). Exporting the TRPCClientContext and TRPCContext would allow the contexts to be used in the useContextBridge hook.

Describe alternate solutions

If we can't get access to the context, we will likely need to patch package or fork and use our own package. (Neither solution is ideal.)

Additional information

No response

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR implementing this feature!

Activity

  1. KATT commented on Apr 7, 2025

    @KATT
    Member

    How come you can't use useTRPC()/useTRPCClient()?

    We're not familiar with the above libraries, can you show some code that isn't working?

  2. mrmattrc commented on Apr 9, 2025

    @mrmattrc
    Author

    @KATT, here's the source code for useContextBridge. And, as you're already familiar with, here is the source code for useTRPC and useTRPCClient (for other readers' context).

    My understanding of useContextBridge is that I need the actual context objects (e.g. TRPCClientContext and TRPCContext) and can't use the values from the "useContext" hooks.

    As for our actual code example, here is our custom Html component that needs to have access to the context values.

    import { cn } from '@core/ui/lib/utils'
    import { Html as BaseHtml, useContextBridge } from '@react-three/drei'
    import type { ComponentProps, RefObject } from 'react'
    import { EngineContext, useEngineSelector } from '../engine/engine-store'
    
    type HtmlProps = ComponentProps<typeof BaseHtml> & Readonly<{ contentClassName?: string }>
    
    export function Html({ className, contentClassName, children, ...props }: HtmlProps) {
    	const [portalRef] = useEngineSelector((state) => [state.portalRef])
    	const ContextBridge = useContextBridge(
    		EngineContext,
    		// TRPCContext, // We need to be able to pass the context in here so that children can still call trpc hooks.
    		// TRPCClientContext, // We need to be able to pass the context in here so that children can still call trpc hooks.
    	)
    
    	return (
    		<BaseHtml
    			zIndexRange={[10, 0]}
    			// pointer-events-none is necessary to prevent the canvas from capturing mouse events (i.e. moving the scene)
    			className={cn('pointer-events-none relative', className)}
    			{...props}
    			portal={portalRef as RefObject<HTMLDivElement>}
    		>
    			<ContextBridge>
    				{/* pointer-events-auto allows the children components to capture mouse events (i.e. moving a Slider for a SBS scene) */}
    				<div className={cn('pointer-events-auto', contentClassName)}>{children}</div>
    			</ContextBridge>
    		</BaseHtml>
    	)
    }
  3. KATT commented on Apr 9, 2025

    @KATT
    Member

    What's the point with that useContextBridge()-hook..? What does it do?

    I'm struggling to see why this hook is needed versus having an old fashioned provider pyramid at the root

  4. mrmattrc commented on Apr 12, 2025

    @mrmattrc
    Author

    @KATT, per the R3F Drei docs:

    Allows you to forward contexts provided above the to be consumed from within the normally.

    The Html component is required to render non-R3F components (e.g. any normal React component) inside Canvas. Technically, I believe it creates a portal, so the portaled children no longer have access to the contexts. Being able to forward the contexts to useContextBridge is necessary to call tRPC hooks inside children.

  5. KATT commented on Nov 2, 2026

    @KATT
    Member

    🤖 In v12, createTrpcContext() returns the React context next to the provider and hooks ([TrpcProvider, useTrpc, useTrpcClient, context]), so you can hand context to Drei's useContextBridge and use the tRPC hooks inside <Html>. https://v12.trpc.io/v12/docs/integrations/tanstack-query/react#a-client-through-context. v12 isn't released yet and there's no ETA.

  6. added
    next-major/definite
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions