Utilizing React and TypeScript: Passing Arguments to MouseEventHandler Type Event Handlers

Can you help me understand how to properly define the event handler handleStatus as type MouseEventHandler, in order to pass an additional argument of type Todo to the function?

interface TodoProps {
  todos: Array<Todos>        
  handleStatus: MouseEventHandler,
  index: number,
  className: string
}
    
export const Todo: FunctionComponent<TodoProps> = ({ todos, handleDeleteTodo, handleStatus, index, className }): ReactElement => {
  const d_todo: Todos = todos[index];
  return(
    <> 
      <div className= { className } key={ todos[index].id.toString() }>
        {todos[index].describtion}
        <Button lable='' disabled= { false } onClick= { () => handleStatus(todos[index])  }  />
      </div>
    </>
  );
}

I encountered this error:

ERROR in src/components/Todo.tsx:29:84
TS2345: Argument of type 'Todos' is not assignable to parameter of type 'MouseEvent<Element, MouseEvent>'.
  Type 'Todos' is missing the following properties from type 'MouseEvent<Element, MouseEvent>': altKey, button, buttons, clientX, and 29 more.
    27 |             <div className= { className } key={ todos[index].id.toString() }>
    28 |                 {todos[index].describtion}
  > 29 |                 <Button lable='' disabled= { false } onClick= { () => handleStatus(todos[index])  }  />
       |                                                                                    ^^^^^^^^^^^^
    30 |                 
    31 |             </div>
    32 |         </>

Answer №1

To handle both the MouseEvent event and a Todo, you should define handleStatus as a function that accepts an event and a Todo:

handleStatus: (event: MouseEvent, todo: Todo) => void

Then in your component:

onClick={(event) => handleStatus(event, todos[index])}

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

Using NestJS to populate data will only populate the first element

I have a Mongoose schema in NestJS structured like this: ... @Prop() casinoAmount: number; @Prop() gameHyperLink: string; @Prop() casinoHyperLink: string; @Prop({ type: Types.ObjectId, ref: 'Game' }) games: Game[]; } I'm t ...

No change in the URL when using Material-UI's BottomNavigation component

I have been attempting to craft a Bottom Navigation bar using material-ui. However, I am facing an issue where clicking on each tab does not impact the URL on my web page. In the beginning, I was utilizing the Link component from React which allowed me to ...

Angular2's hidden feature isn't functioning properly

There is a common suggestion to use *ngIf better than [hidden] but in my scenario, I want to keep the element in the DOM without it being visible. In my component.html file, I have: <article #articleId id="bodyArticle" [hidden]="isClicked"></art ...

Tips for passing certain optional parameters while excluding others without resorting to undefined or empty values like ""

Is there a way to invoke the function while omitting certain optional parameters without resorting to undefined and empty strings? import { MouseEvent } from "react"; import { DialogType } from "editor-constants"; export interface Dial ...

Injecting personalized themes into withStyles

I've designed a theme that I'm attempting to incorporate into my useStyles. Here's the theme: import { createMuiTheme } from "@material-ui/core/styles"; const customTheme = createMuiTheme({ palette: { primary: { ...

Issues with NextJS detecting environmental variables

I recently encountered an issue with my NextJS app running on Next.js v12.2.5 where it appears to be ignoring the environment variables I've configured. To address this, I created a .env.local file with the following content: NEXT_PUBLIC_SERVER_URL=h ...

Challenges with file types in the build directory of a React Express application

My website was running smoothly until yesterday, but now I am encountering some issues. It is a React app built with Express and NodeJS. While the website works fine in development mode using yarn start, I am facing errors in production: GET https://www.m ...

Encountered Typescript issue when utilizing typed forms in Angular

Below is the interface I am working with: export interface ILoginDto { email: string; password: string; } Here is a snippet of the relevant code from the component: import { FormBuilder, FormGroup, Validators } from '@angular/forms'; export ...

Frontend Axios POST request not being received in Node.js backend

I'm currently diving into the world of node js and attempting to make a post request from axios through my frontend, but I'm running into an issue where node js is returning an empty object. Below is the code snippet: Node JS var express = requ ...

Error message: "Angular requires either importing or local installation"

For my ionic application development, I encountered an issue while trying to link pages together in the ionic creator. The error message on the .ts file is: typescript: src/pages/home/home.ts, line: 4 Individual declarations in merged declar ...

React isn't updating the on-change value despite changes being made

In my React application, there is a file called EditTodos.js that is responsible for updating the to-do list. When I click on the "Edit" button, it triggers a pop-up modal component. import React, { useState } from "react"; import { Button, Modal } from " ...

Where can I find a package or source for HTML markup validation that is compatible with React projects and specifically supported on React?

Currently, I am in the process of developing a project using React and I am looking to incorporate HTML markup validation into user input. Does anyone know of a source or package that supports this feature for use with React? I appreciate any assistance i ...

Redesigning the reset Zoom feature of HighCharts using a button inspired by Material UI styling

Currently, I am developing a project that incorporates HighCharts and Material UI for the user interface design components. I am wondering if there is a method to substitute the default HighChart reset Zoom button with the Material UI button component? ...

What is the best way to divide data prior to uploading it?

I am currently working on updating a function that sends data to a server, and I need to modify it so that it can upload the data in chunks. The original implementation of the function is as follows: private async updateDatasource(variableName: strin ...

TypeScript functions with Generic Constraints return specific values rather than just types

function createGenericCoordinates<Type extends number | string>( x: Type, y: Type ) { return { x, y }; } const genericCoordinates = createGenericCoordinates(1, 2); // Error (TS2322) // Type 3 is not assignable to type 1 | 2 genericCoordinates ...

Guide to accessing a newly opened window from a different domain originating from the current window

Currently working on an Angular project, I am facing a scenario where I have a link on page A that directs users to a different origin page B. The HTML code for the link is shown below: ... <a href="https://another.origin"> PAGE B </a> ... On ...

Error: React Select input control is throwing a TypeError related to event.target

Having trouble changing the state on change using a React Select node module package. It works with regular text input, but I can't quite get it to work with this specific component. The error message "TypeError: event.target is undefined" keeps poppi ...

Run JavaScript code whenever the table is modified

I have a dynamic table that loads data asynchronously, and I am looking for a way to trigger a function every time the content of the table changes - whether it's new data being added or modifications to existing data. Is there a method to achieve th ...

Error message: "The source and destination cannot be identical in the AWS Amplify React JS Next application."

I am facing an issue while attempting to deploy my Reactjs Next Application on AWS Amplify as it displays an error during the build process. Here is a snippet of my amplify.yml configuration for pre-build and build: version: 1 frontend: phases: preB ...

Tips for Using Typescript Instance Fields to Prevent Undefined Values

After creating a few Typescript classes, I encountered an issue where I would get an undefined error when trying to use them after instantiating. I experimented with initializing my fields in the constructor, which resolved the problem, but I don't t ...