tRPCttRPC
Powered by
Alan_sztA
tRPC•2y ago•
2 replies
Alan_szt

How to get access to the types generated by the server in the frontend (React)?

In this example, how do you replace
data: any
data: any
to the correct type?

import { trpc } from "../utils/trpc";

type Props = {
  onSuccess: (data: any) => void;
};
export function CreateWorker(props: Props) {
  const createWorkerMutation = trpc.createWorker.useMutation();

  const handleCreateWorker = () => {
    createWorkerMutation.mutate(
      { data: "hello" },
      {
        onSuccess: (data) => props.onSuccess(data),
      }
    );
  };
  return <button onClick={handleCreateWorker}>create</button>;
}
import { trpc } from "../utils/trpc";

type Props = {
  onSuccess: (data: any) => void;
};
export function CreateWorker(props: Props) {
  const createWorkerMutation = trpc.createWorker.useMutation();

  const handleCreateWorker = () => {
    createWorkerMutation.mutate(
      { data: "hello" },
      {
        onSuccess: (data) => props.onSuccess(data),
      }
    );
  };
  return <button onClick={handleCreateWorker}>create</button>;
}
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

How is the Client Generated with just the types
Mohammed AnasMMohammed Anas / ❓-help
3y ago
React tsc starts checking server types
MarksmithMMarksmith / ❓-help
3y ago
Types not being shared with the frontend
scrubbuddySscrubbuddy / ❓-help
2y ago