You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
@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{HtmlasBaseHtml,useContextBridge}from'@react-three/drei'importtype{ComponentProps,RefObject}from'react'import{EngineContext,useEngineSelector}from'../engine/engine-store'typeHtmlProps=ComponentProps<typeofBaseHtml>&Readonly<{contentClassName?: string}>exportfunctionHtml({ className, contentClassName, children, ...props}: HtmlProps){const[portalRef]=useEngineSelector((state)=>[state.portalRef])constContextBridge=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(<BaseHtmlzIndexRange={[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={portalRefasRefObject<HTMLDivElement>}><ContextBridge>{/* pointer-events-auto allows the children components to capture mouse events (i.e. moving a Slider for a SBS scene) */}<divclassName={cn('pointer-events-auto',contentClassName)}>{children}</div></ContextBridge></BaseHtml>)}
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.
Activity
KATT commentedon Apr 7, 2025
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?
mrmattrc commentedon Apr 9, 2025
@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
useContextBridgeis 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
Htmlcomponent that needs to have access to the context values.KATT commentedon Apr 9, 2025
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
mrmattrc commentedon Apr 12, 2025
@KATT, per the R3F Drei docs:
The
Htmlcomponent is required to render non-R3F components (e.g. any normal React component) insideCanvas. Technically, I believe it creates a portal, so the portaled children no longer have access to the contexts. Being able to forward the contexts touseContextBridgeis necessary to call tRPC hooks inside children.KATT commentedon Nov 2, 2026
🤖 In v12,
createTrpcContext()returns the React context next to the provider and hooks ([TrpcProvider, useTrpc, useTrpcClient, context]), so you can handcontextto Drei'suseContextBridgeand 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.