Full-stack & typesafe React (+Native) apps with tRPC.io

Rate this content
Bookmark

Why are we devs so obsessed with decoupling things that are coupled nature? tRPC is a library that replaces the need for GraphQL or REST for internal APIs. When using it, you simply write backend functions whose input and output shapes are instantly inferred in your frontend without any code generation; making writing API schemas a thing of the past. It's lightweight, not tied to React, HTTP-cacheable, and can be incrementally adopted. In this talk, I'll give a glimpse of the DX you can get from tRPC and how (and why) to get started.

This talk has been presented at React Advanced 2021, check out the latest edition of this React Conference.

FAQ

In the context of HTTP, an API (Application Programming Interface) is a method of calling functions on another computer, allowing different systems to communicate with each other.

tRPC is a toolkit designed to create end-to-end type-safe APIs in a simplified manner, particularly for React applications. It enables automatic type inference from backend to frontend, enhancing developer productivity and reducing errors.

With tRPC, you typically start by defining your API schema or contract, implement the back-end logic, generate code for the API queries on the front-end, and utilize hooks like 'useQuery' for easy data fetching and state management.

tRPC simplifies API endpoint management by providing type-safe communication and automatic code generation, which leads to better autocompletion, fewer errors, and a more streamlined development process.

Using TypeScript with tRPC offers consistent language across front-end, back-end, and mobile, facilitating type safety and reducing inconsistencies across different parts of an application.

Yes, tRPC is designed for incremental adoption, allowing projects to start integrating it at their own pace without needing to overhaul existing codebases.

To create a backend with tRPC, begin by establishing a router and nesting your queries or procedures within it. Each procedure functions as an endpoint that performs specific operations and returns data.

Yes, tRPC is compatible with React Native, allowing developers to use the same API development tools and methodologies across web and mobile applications.

Alexander Johansson
Alexander Johansson
6 min
22 Oct, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Alex introduces tRPC, a toolkit for making end-to-end type-safe APIs easily, with auto-completion of API endpoints and inferred data from backend to frontend. tRPC works the same way in React Native and can be adopted incrementally. The example showcases backend communication with a database using queries and validators, with types inferred to the frontend and data retrieval done using Prisma ORM.

1. Introduction to tRPC and API Development

Short description:

Hello, I'm Alex. I'm here to talk about full stack, React, and tRPC. I've been making websites for a really long time and been working with React since 2016 or so. I'm here to introduce tRPC, a toolkit for making end-to-end type-safe API in a very easy manner. When you write tRPC back-end, you have a router where you nest all your queries. And when we use this in our front-end, we get auto-completion of all our API endpoints and the data inferred from the function in the back-end to the front-end.

Hello, I'm Alex. I'm here to talk about full stack, React, and tRPC. I've been making websites for a really long time and been working with React since 2016 or so. I've also worked a bit as an iOS developer and whatnot.

And today I'm here to talk about APIs. I know this is a Content Conference, but just bear with me. You'll realize it's not that hard. So, to break it down, let's start with the question, what is an API? So, in the context of HTTP, very simplified, an API is just a way of calling functions on another computer. And the way we make and use APIs today might look something like this. We start with an API contract or schema, because we want to have some guarantee that our API is actually returning what we want it to return. And then we might do some code generation in order to help our back end logic on what it needs to return. Then once we've done our back end, we write an API query on our front end. Which then might have some code generation attached to it to generate a nice use query hook that we can use at the end. So, I think there's quite a few steps to doing an API today. And the reason for this, as I see it, is that we have decoupled our back-end and front-end completely, or rather that we have made them disjointed. But what if our back-end and front-end didn't have to be decoupled? There are tools today where we can write the same sort of language on the front-end and back-end and mobile. And one of those tools is TypeScript. So, TypeScript is a good language which you can use everywhere, and I've been working hard over the last ten months to create some set of tools to simplify the sort of API development with your React apps. And, yeah, I'm here to introduce tRPC. tRPC is a toolkit for making end-to-end type-safe API in a very easy manner. I'm just going to dig in to show you how it looks like. So, when you write tRPC back-end, you start something like this. You have a router where you nest all your queries. Here is a query called hello. We call this a procedure in tRPC. You can also view it as an endpoint. And all that this function does is to return some data. And when we then use this in our front-end, we get straightaway auto-completion of all our API endpoints and we get the data inferred from your function in the back-end to your front-end. And this doesn't rely on just a bit of an abuse on the TypeScript compiler. And it's a thin wrapper around ReactQuery.

2. tRPC in React Native and Complex Example

Short description:

tRPC works the same way in React Native. You can adopt it incrementally or not at all. In this example, the backend communicates with a database using a post by ID query and a validator for input data. The types are inferred to the frontend, and the ORM Prisma is used to retrieve data from the database.

And what you see here works exactly the same in React Native. And if you want to, you can incrementally adopt tRPC to your project, or you cannot use it at all if you don't want to. So let's see a bit more complex example. Here we have a backend that is actually talking to a database. So you can see here that we have a post by ID query, and that takes some input data through a validator, and you can see that the types of that validator is inferred straight away to the front end. And then at the end, you can see that we are talking to our database through an ORM called Prisma, and that gets some data back that is also then inferred straight away on the frontend.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Guide to React Rendering Behavior
React Advanced 2022React Advanced 2022
25 min
A Guide to React Rendering Behavior
Top Content
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize React rendering.
Building Better Websites with Remix
React Summit Remote Edition 2021React Summit Remote Edition 2021
33 min
Building Better Websites with Remix
Top Content
Remix is a web framework built on React Router that focuses on web fundamentals, accessibility, performance, and flexibility. It delivers real HTML and SEO benefits, and allows for automatic updating of meta tags and styles. It provides features like login functionality, session management, and error handling. Remix is a server-rendered framework that can enhance sites with JavaScript but doesn't require it for basic functionality. It aims to create quality HTML-driven documents and is flexible for use with different web technologies and stacks.
React Compiler - Understanding Idiomatic React (React Forget)
React Advanced 2023React Advanced 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
Watch video: React Compiler - Understanding Idiomatic React (React Forget)
Joe Savona
Mofei Zhang
2 authors
The Talk discusses React Forget, a compiler built at Meta that aims to optimize client-side React development. It explores the use of memoization to improve performance and the vision of Forget to automatically determine dependencies at build time. Forget is named with an F-word pun and has the potential to optimize server builds and enable dead code elimination. The team plans to make Forget open-source and is focused on ensuring its quality before release.
Using useEffect Effectively
React Advanced 2022React Advanced 2022
30 min
Using useEffect Effectively
Top Content
Today's Talk explores the use of the useEffect hook in React development, covering topics such as fetching data, handling race conditions and cleanup, and optimizing performance. It also discusses the correct use of useEffect in React 18, the distinction between Activity Effects and Action Effects, and the potential misuse of useEffect. The Talk highlights the benefits of using useQuery or SWR for data fetching, the problems with using useEffect for initializing global singletons, and the use of state machines for handling effects. The speaker also recommends exploring the beta React docs and using tools like the stately.ai editor for visualizing state machines.
Routing in React 18 and Beyond
React Summit 2022React Summit 2022
20 min
Routing in React 18 and Beyond
Top Content
Routing in React 18 brings a native app-like user experience and allows applications to transition between different environments. React Router and Next.js have different approaches to routing, with React Router using component-based routing and Next.js using file system-based routing. React server components provide the primitives to address the disadvantages of multipage applications while maintaining the same user experience. Improving navigation and routing in React involves including loading UI, pre-rendering parts of the screen, and using server components for more performant experiences. Next.js and Remix are moving towards a converging solution by combining component-based routing with file system routing.
(Easier) Interactive Data Visualization in React
React Advanced 2021React Advanced 2021
27 min
(Easier) Interactive Data Visualization in React
Top Content
This Talk is about interactive data visualization in React using the Plot library. Plot is a high-level library that simplifies the process of visualizing data by providing key concepts and defaults for layout decisions. It can be integrated with React using hooks like useRef and useEffect. Plot allows for customization and supports features like sorting and adding additional marks. The Talk also discusses accessibility concerns, SSR support, and compares Plot to other libraries like D3 and Vega-Lite.

Workshops on related topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
Concurrent Rendering Adventures in React 18
React Advanced 2021React Advanced 2021
132 min
Concurrent Rendering Adventures in React 18
Top Content
Featured WorkshopFree
Maurice de Beijer
Maurice de Beijer
With the release of React 18 we finally get the long awaited concurrent rendering. But how is that going to affect your application? What are the benefits of concurrent rendering in React? What do you need to do to switch to concurrent rendering when you upgrade to React 18? And what if you don’t want or can’t use concurrent rendering yet?

There are some behavior changes you need to be aware of! In this workshop we will cover all of those subjects and more.

Join me with your laptop in this interactive workshop. You will see how easy it is to switch to concurrent rendering in your React application. You will learn all about concurrent rendering, SuspenseList, the startTransition API and more.
React Hooks Tips Only the Pros Know
React Summit Remote Edition 2021React Summit Remote Edition 2021
177 min
React Hooks Tips Only the Pros Know
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
The addition of the hooks API to React was quite a major change. Before hooks most components had to be class based. Now, with hooks, these are often much simpler functional components. Hooks can be really simple to use. Almost deceptively simple. Because there are still plenty of ways you can mess up with hooks. And it often turns out there are many ways where you can improve your components a better understanding of how each React hook can be used.You will learn all about the pros and cons of the various hooks. You will learn when to use useState() versus useReducer(). We will look at using useContext() efficiently. You will see when to use useLayoutEffect() and when useEffect() is better.
React, TypeScript, and TDD
React Advanced 2021React Advanced 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Mastering advanced concepts in TypeScript
React Summit US 2023React Summit US 2023
132 min
Mastering advanced concepts in TypeScript
Top Content
Featured WorkshopFree
Jiri Lojda
Jiri Lojda
TypeScript is not just types and interfaces. Join this workshop to master more advanced features of TypeScript that will make your code bullet-proof. We will cover conditional types and infer notation, template strings and how to map over union types and object/array properties. Each topic will be demonstrated on a sample application that was written with basic types or no types at all and we will together improve the code so you get more familiar with each feature and can bring this new knowledge directly into your projects.
You will learn:- - What are conditional types and infer notation- What are template strings- How to map over union types and object/array properties.
Web3 Workshop - Building Your First Dapp
React Advanced 2021React Advanced 2021
145 min
Web3 Workshop - Building Your First Dapp
Top Content
Featured WorkshopFree
Nader Dabit
Nader Dabit
In this workshop, you'll learn how to build your first full stack dapp on the Ethereum blockchain, reading and writing data to the network, and connecting a front end application to the contract you've deployed. By the end of the workshop, you'll understand how to set up a full stack development environment, run a local node, and interact with any smart contract using React, HardHat, and Ethers.js.