react-query + zod validation

Solution retrospective
zod validation
// utils.ts import { z } from "zod"; const adviceSchema = z.object({ slip: z.object({ id: z.number(), advice: z.string(), }), }); export const getAdvice = async () => { const data = await fetch("https://api.adviceslip.com/advice").then((d) => d.json() ); return adviceSchema.parse(data); }; // main.tsx const queryClient = new QueryClient(); ReactDOM.createRoot(document.getElementById("root")!).render( ); //App.tsx import { useQuery } from "@tanstack/react-query"; const { data, refetch } = useQuery({ queryKey: ["get_advice"], queryFn: getAdvice, });
Please log in to post a comment
Log in with GitHubCommunity feedback
No feedback yet. Be the first to give feedback on Zup's solution.
Join our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord