Problem encountered with the onSuccess callback in the Next.js useQuery hook caused by tprc.authCallback

Encountering an issue with the onSuccess callback and useQuery hook from trpc.authCallback in a Next.js app. Despite defining the success parameter type, TypeScript throws an error claiming onSuccess is not found in provided options.

My code snippet:

import { useRouter, useSearchParams } from "next/navigation";
import { trpc } from "../_trpc/client";

const Page = () => {
  const router = useRouter();
  const searchParams = useSearchParams();
  const origin = searchParams.get("origin");
  
  const { data, isLoading } = trpc.authCallback.useQuery(undefined, {
    // Explicitly define type for success to fix TypeScript error
    onSuccess: ({ success }: { success: boolean }) => {
      if (success) {
        router.push(origin ? `/${origin}` : '/dashboard');
      }
    }
  });

  return null; // could also show loading indicator here
};

export default Page;

The error message states:

No overload matches this call.
...
(property) onSuccess: ({ success }: {
    success: boolean;
}) => void

How can I resolve this issue and successfully implement the onSuccess callback with the useQuery hook in my Next.js application? Appreciate any assistance!

Attempts made:

Defined the success parameter type within the onSuccess callback. Reviewed trpc.authCallback.useQuery documentation for specific requirements regarding onSuccess. Searched online for similar issues but found no solution.

Expectations:

Expected that by specifying the success type in the onSuccess callback, TypeScript would recognize it correctly and run the code without errors.

Answer №1

It appears that I have identified the issue you are encountering. The @tanstack/react-query package in the tutorial you are referencing is version 4. However, starting from version 5, the onSuccess, onError, and onSettled callbacks have been removed, as highlighted in a blog post by tkkudo, who is a co-maintainer of the project. I recommend checking out his blog post for more information.

To address your issue, instead of using the deprecated onSuccess callback, you can try something like this:

Update: It seems that you now have access to isSuccess, isError, and isPending, which provides a cleaner approach.

const { data, isLoading, isError, isSuccess } = trpc.authCallback.useQuery();
if (isSuccess) {//Your code} if (isError) {//Your code}

Update: Alternatively, you can also revert to using the old package if you prefer following the tutorial exactly as shown in the video.

Answer №2

    "@tanstack/react-query": "^4.35.3",

Please utilize the specified version of react-query and attempt to re-install the dependencies.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A guide on converting JSON to TypeScript objects while including calculated properties

I have a standard JSON service that returns data in a specific format. An example of the returned body looks like this: [{ x: 3, y: 5 }] I am looking to convert this data into instances of a customized TypeScript class called CustomClass: export class ...

Unlock specific elements within the "sub-category" of a combined collection

If my union type is structured like this: type StateUpdate = { key: 'surname', value: string } | { key : 'age', value: number }; This setup is convenient because it allows me to determine the type of the value based on the key. Howev ...

Utilize the key types of an object to validate the type of a specified value within the object

I am currently working with an object that contains keys as strings and values as strings. Here is an example of how it looks: const colors = { red: '#ff0000', green: '#00ff00', blue: '#0000ff', } Next, I define a type ...

Tips for assigning a JSON object as the resolve value and enabling autosuggestion when utilizing the promise function

Is there a way to make my promise function auto-suggest the resolved value if it's a JSON object, similar to how the axios NPM module does? Here is an example of how axios accomplishes this: axios.get("url.com") .then((res) => { Here, axios will ...

Tips for implementing Nested Interface values within the Grid

I am facing an issue trying to retrieve values from a nested interface. Currently, I am only getting null for the nested interface values, while other values are being retrieved successfully. import {CyAndNY} from "./CyAndNYInterface"; export interface G ...

Looking for guidance on how to deploy a Node server with TypeScript on Vercel

I keep encountering a Code: NOT_FOUND error on my server while running the endpoint. The issue seems to be related to the configuration setup of my TypeScript-based Node server, and I've been struggling with it for quite some time now. Additionally, ...

The issue with Next.js dynamic routes failing on production post-refresh

I'm facing an issue with dynamic routes in Next.js when using Firebase hosting. While everything works smoothly in the local environment with emulators, deploying the application using firebase deploy results in the dynamic route not being directly ac ...

Adjusting ngClass dynamically as values change

Currently, I am facing a situation where I want to dynamically add a class to my view using ngClass based on changes in the output value. The output value is dependent on the response received from an API, and I am fetching data from the endpoint every sec ...

When entering the website URL directly into the browser, the query parameters are being lost

Whenever I enter https://localhost/explore?name=teste in my browser, it redirects me to https://localhost/explore, causing the query parameters to be lost. However, when I click on a button, I am correctly redirected to https://localhost/explore?name=test ...

The error message indicates that the argument cannot be assigned to the parameter type 'AxiosRequestConfig'

I am working on a React app using Typescript, where I fetch a list of items from MongoDB. I want to implement the functionality to delete items from this list. The list is displayed in the app and each item has a corresponding delete button. However, when ...

Refill ag-grid with fresh data

Setting up the ag-grid initialization directly from the HTML using an onGridReady method in the component file. <div style="flex-grow:1;"> <ag-grid-angular style="float:left;width: 100%; height: 201px;margin-top:10px;" class="ag- ...

Problem encountered while implementing callbacks in redux-saga

I am facing a scenario in which I have a function called onGetCameras that takes a callback function named getCamerasSuccess. The idea is to invoke the external function onGetCameras, which makes an AJAX call and then calls getCamerasSuccess upon completio ...

The switch statement and corresponding if-else loop consistently produce incorrect results

I'm currently facing an issue where I need to display different icons next to documents based on their file types using Angular framework. However, no matter what file type I set as the fileExtension variable (e.g., txt or jpg), it always defaults to ...

Problems with updating HTML/Typescript in Visual Studio and Chrome are causing frustration

While working on my company's application locally and making HTML/TS changes, I am facing an issue. Whenever I save/hot reload and refresh the browser, no changes seem to take effect. I've tried stopping debugging, building/rebuilding, and runni ...

How to modify the background color within the mat-menu-panel

Incorporating angular 9 and less into my current project, I have encountered an issue with a mat-menu-panel where my mat-menu-item is located. While I have successfully changed the color of my mat-menu-item, I am now faced with the challenge of changing th ...

Jest - managing parent class methods during unit tests

The code snippet provided demonstrates a class called First extending TelemetryFramework with specific props and states, containing a method named getData. This method retrieves confidential data and logs telemetry information. However, running unit tests ...

Enhancing asynchronous operations with TypeScript and ExpressJs using Async/Await

Exploring the benefits of TypeScript's async/await in an Express project, I came across the following code snippet that seems to be stuck indefinitely without producing any outcome. Any thoughts on how to fix this issue? .. router.get('/test ...

Utilize FieldPath.documentId() from Firestore to access a nested object

Let me explain the structure of my data: Each element contains a cluster object, and the cluster object includes a tags object with one or more tag objects. This setup aligns with firebase's denormalized data structure, as outlined here. We implemen ...

Unable to sort dates using arrays on IOS

Currently, I am retrieving data from my server and storing it in a variable named "items". Whenever I execute the following code: if (this.items) { this.items.sort(function (a, b) { return +new Date(b.datum) - ...

Problem: Tailwind CSS styles are not being applied on responsive screens in Next.js.Issue: Despite

I'm attempting to apply a custom class to a div that should only be active on "md" screens. However, it doesn't seem to be working - <div className="md:myclass"/> tailwind-config.ts theme: { screens:{ 'sm': { ...