Issue with MUI icon import: React, Typescript, and MUI type error - Overload does not match this call

Within my component, I am importing the following:

import LogoutIcon from "@mui/icons-material/Logout";
import useLogout from "@/hooks/auth/useLogout";

const { trigger: logoutTrigger } = useLogout();

However, when utilizing this component as shown below, an error is highlighted:

            <LogoutIcon
              type="button"
              onClick={logoutTrigger}
              sx={LogoutIconSx}
              cursor="pointer"
            />

The full error message is as follows:

(property) onClick: <SWRData = any>(extraArgument?: null | undefined, options?: SWRMutationConfiguration<any, any, never, "/logout", SWRData> | undefined) => Promise<any>
No overload matches this call.
  (...)

In addition to that, here is the snippet of the hook being used:

const cookies = new Cookies();

const fetcher = (url: string) => axios.post(url).then((res) => res.data);

export default function useLogout() {
  const router = useRouter();

  const { trigger, error } = useSWRMutation("/logout", fetcher, {
    onSuccess: () => {
      cookies.remove(ACCESS_TOKEN_COOKIE_NAME);
      router.push("/login");
    },
  });

  return {
    trigger,
    error,
  };
}

Upon further investigation, I found that changing onClick={logoutTrigger} to

onClick={() => console.log(test}
resolves the issue.

A temporary workaround involves setting onClick={logoutTrigger as any}, eliminating the error at hand. However, ideally, I want to avoid type casting.

For context, I am using MUI v5. Can someone shed light on why this error is occurring and suggest how to rectify it?

Answer №1

The issue likely arises from attempting to pass the event object as an argument in the onClick event handler to the logoutTrigger function, which does not accept an event object.

You can try using the code below, as it may resolve the problem:

<LogoutIcon
  type="button"
  onClick={() => logoutTrigger()}
  sx={LogoutIconSx}
  cursor="pointer"
/>

Answer №2

When the onClick event is triggered, you attempt to pass its parameters into the trigger function.

Think of it in this way:

trigger(onClickArgs); // This explains why TypeScript shows that there is no match for the function call.

To resolve this, utilize the inline syntax:

onClick={() => trigger()}

This way, you disregard the onClick arguments and invoke the trigger function without any additional parameters being added.

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

Learn the method for triggering events with a strongly-typed payload in Vue 3 Composition API and TypeScript

I'm currently exploring Vue 3 Composition API along with TypeScript, particularly focusing on emitting events with a strictly typed payload. There's an example provided below, but I'm unsure if it's the most effective way to achieve t ...

Encountering a 403 error when attempting to upload files to Google Cloud Storage (GCS) using Signed URLs

The main aim is to create a signed URL in the api/fileupload.js file for uploading the file to GCS. Then, retrieve the signed URL from the Nextjs server through the nextjs API at localhost://3000/api/fileupload. Finally, use the generated signed URL to upl ...

While interacting with router.query, certain characters may be omitted, however they still persist in the URL

Utilizing router.query, I fetch the necessary token for users to update their passwords. However, when I display it in my hidden field, characters such as + and / are removed. Despite attempting to use encodeURIComponent to address this issue, it does not ...

The module "ng-particles" does not have a Container component available for export

I have integrated ng-particles into my Angular project by installing it with npm i ng-particles and adding app.ts import { Container, Main } from 'ng-particles'; export class AppComponent{ id = "tsparticles"; /* Using a remote ...

The art of expanding Angular's HTTP client functionality

I understand that the following code is not valid in Angular, but I am using it for visual demonstration purposes. My goal is to enhance the Angular HTTP client by adding custom headers. I envision creating a class like this, where I extend the Angular h ...

What is the process for transforming a multi-dimensional array containing strings into a multi-dimensional array containing numbers?

I've got a unique structure of data composed of arrays with strings as seen below: [ 0: Array(1) 0: Array(6) 0: [5.379856, 43.252967] 1: [5.422988, 43.249466] 2: [5.425048, 43.245153] 3: [5.383804, 43.239 ...

ES6 syntax specification allows for the use of a fat arrow function for declaring React components

When learning React, I have noticed two different ways of declaring components. The first is using the classic fat arrow syntax with a return statement. const Component = () => { return ( <div>Hello</div> ) } Recently, I came ...

ESLint only lints certain errors in the command-line interface

Incorporating eslint into my project has been a game-changer. Here's how my .eslintrc file is set up: // http://eslint.org/docs/rules { "parser": "babel-eslint", "env": { "browser": true, "node": true, "mocha": true }, "plugins": ...

Tips for creating a cookie for an alternate website upon launching a new browser tab

Hey there, I'm facing an issue that I could really use some help with. To give you some context, I'm working on a project using Angular and TypeScript. My goal is to implement single sign-on functionality for multiple websites within one applica ...

Guide on setting a default value for a variable within a Typescript class

In the process of developing a component to manage information about fields for form use, I am in need of storing different data objects in order to establish generic procedures for handling the data. export class DataField<T> { /** * Field ...

issue with displaying popover component using React with Material UI

A React component I created has 6 different experiences, each triggering a popover with an array of images. The popovers are implemented using Material UI, and while they work, there are some console errors that I'm unsure how to resolve. Below is th ...

What is the best way to retrieve data from app.post within react.js?

//server.js app.post('/trip', function(req,res){ var params = "something"; getResult(params).then((db)=>{ // I am trying to access the variable called "db" in my App.js(React) file, but I am unsure of how to do so. res.s ...

Enhabling Effortless Button Activation & Sustained Navigation State: Integrating Dynamic Navigation in React

"I am facing a React challenge and seeking assistance to implement a specific functionality with a button. At present, the button starts with a false state, but I intend for it to automatically activate and reveal a navigation component (nav) when the ...

Tips for simulating changing data in snapshot tests?

Two challenge are puzzling me currently: The snapshot test for a router page includes custom routers with guards specifically for logged in users. Each time I run this test, a different generated key is produced. I am using new Date() in a date pick ...

The dropdown menu in Mantine is malfunctioning, even though I copied and pasted the exact code from

While following a tutorial, I encountered an issue with the Mantine Menu and Dropdown components. The tutorial creator did not wrap the React App inside the MantineProvider, which resulted in errors when trying to use the Dropdown component. Even after add ...

Is it possible to center an anchor link in React JS using Webpack, Sass, and Bootstrap 4?

Recently, I started delving into React JS and Webpack but encountered a perplexing issue. My goal is simple - to center an anchor link on the screen. However, despite trying various methods like inspecting the element, removing inherited styles, setting wi ...

Creating a personalized fake database functionality in Angular

Is there a way to implement the fake-db feature in Angular while utilizing this resource? I need it to support an API response structure like the one below for list retrieval. // GET 'api/outlets' { data: [ {'id':1, 'name&ap ...

The Mui DataGrid React has surpassed the maximum call stack limit

One of the DataGrid tables in my project seems to be causing issues, and I can't seem to pinpoint the problem. To provide a clearer understanding, I have prepared a codesandbox demonstration showcasing the issue. Any assistance or insights would be g ...

Encountered an error involving the SequenceExpression node type while using Jest

While adding a snapshot test to my React code, I encountered an unexpected error message: Unexpected node type: SequenceExpression (This is an error on an internal node. Probably an internal error. Location has been estimated.) The code transpiles withou ...

Guide to aligning a component in the middle of the screen - Angular

As I delve into my project using Angular, I find myself unsure about the best approach to rendering a component within the main component. Check out the repository: https://github.com/jrsbaum/crud-angular See the demo here: Login credentials: Email: [e ...