Poonda
Best Monorepo Pattern for Sharing tRPC Logic Across Apps
Hey tRPC community!
I'm working on a monorepo with a standalone tRPC server, a React Native app, and a Next.js app. Both frontend apps share a lot of common logic, particularly custom React hooks that wrap tRPC calls.
Currently, my proposed solution is to create shared hooks that require a React context providing the tRPC client. This approach allows each app to instantiate its own tRPC client (both based on the same AppRouter) while maintaining shared logic.
Specifically:
- Each app creates its own tRPC client and react-query provider
- Shared hooks live in a separate package
- These hooks require a context that provides the tRPC client
- This allows flexibility for different client configurations
I'm looking for feedback:
1. Are there any potential issues with this approach?
2. Do you know of a more elegant pattern for managing shared tRPC logic in a multi-app monorepo?
Any insights or alternative strategies would be greatly appreciated.
6 replies