Comments (3)
That's not issue with the code in this repo. This is a generic question about how to use tRPC. My recommendation would be to either ask a question on the feedback channels provided by tRPC, or ask in the #react or #typescript channels of the Blue Collar Coder Discord server.
from trpc-on-the-app-router.
hmm
its not really a generic question
let me rephrase:
if you were to use the approach specifically endorsed in this repo
how would you deal with handling TRPC errors globally on the client side
given that with this approach you cant use onError from the createTRPCNext
adapter? (since we are using createTRPCReact
)
thanks for the recommendation -- i have asked a more generic form of the question here: trpc/trpc#4782
from trpc-on-the-app-router.
for when someone else has this issue:
this approach works well enough:
"use client"
import React, { useState } from "react"
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import { httpBatchLink } from "@trpc/client"
import { toast } from "sonner"
import CustomToast from "@/components/common/CustomToast"
import { trpc } from "./trpc-client"
interface IError {
code: string
message: string
}
export default ({ children }: { children: React.ReactNode }) => {
const [queryClient] = useState(
() =>
new QueryClient({
defaultOptions: {
mutations: {
onError: (error) => {
const { code, message } = error as IError
if (code === "BAD_REQUEST") {
toast.custom((t) => (
<CustomToast
variant="error"
message={message}
key={t}
t={t}
/>
))
} else {
toast.custom((t) => (
<CustomToast
variant="error"
message="Something went wrong."
key={t}
t={t}
/>
))
}
},
},
queries: {
onError: (error) => {
const { code, message } = error as IError
if (code === "BAD_REQUEST") {
toast.custom((t) => (
<CustomToast
variant="error"
message={message}
key={t}
t={t}
/>
))
} else {
toast.custom((t) => (
<CustomToast
variant="error"
message="Something went wrong."
key={t}
t={t}
/>
))
}
},
},
},
})
)
const [trpcClient] = useState(() =>
trpc.createClient({
links: [
httpBatchLink({
url: "http://localhost:3000/api/trpc",
}),
],
})
)
return (
<trpc.Provider client={trpcClient} queryClient={queryClient}>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</trpc.Provider>
)
}
from trpc-on-the-app-router.
Related Issues (8)
- Missing package drizzle-orm HOT 2
- createCaller impossible to use with context that requires NextRequest HOT 1
- When using a production build the initial data does not change after the build HOT 1
- TRPCClientError: "input" needs to be an object when doing a batch call HOT 1
- How to create nested routes? HOT 2
- Getting cors error when I try to access the list of todos HOT 1
- nit: Possible misleading context definition in `createCaller`? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from trpc-on-the-app-router.