2

I have problems in declaring callback for onClick property in Dropdown.Item in Typescript. What type event should be. I tried many versions without any luck. I know Dropdown.Item is rendered as an anchor and I want to do it correctly but couldn't find any way to declare type of event parameter. Please help me what type is e in my case. I'm trying MouseEvent<HTMLAnchorElement> but typescript is complaining? What correct type should I use in this place? Of course everything is inside Function Component body etc... but I just extracted my problem here...

import React, { MouseEvent } from 'react';
import { Dropdown } from 'react-bootstrap';

const handleClick = ( e: MouseEvent<HTMLAnchorElement> ) => {
    e.preventDefault();
    // Do something
  };

<Dropdown.Item href="#" role="button" onClick={ handleClick }>
  Item
</Dropdown.Item>

Typescript is complaining with this message:

Type '(e: MouseEvent<HTMLAnchorElement>) => void' is not assignable to type '(event: MouseEvent<ReplaceProps<BsPrefixComponentClass<"a", SafeAnchorProps>, BsPrefixProps<BsPrefixComponentClass<"a", SafeAnchorProps>> & DropdownItemProps>, MouseEvent>) => void'.
      Types of parameters 'e' and 'event' are incompatible.

Should I put all this crazy and long declaration in my callback declaration?

1
  • could you please include the codesandbox Commented Oct 1, 2020 at 18:46

1 Answer 1

5

Check the definition of DropdownItem:

import React from 'react';
import SafeAnchor from './SafeAnchor';
import { BsPrefixPropsWithChildren, BsPrefixRefForwardingComponent, SelectCallback } from './helpers';
export interface DropdownItemProps extends BsPrefixPropsWithChildren {
    active?: boolean;
    disabled?: boolean;
    eventKey?: string;
    href?: string;
    onClick?: React.MouseEventHandler<this>;
    onSelect?: SelectCallback;
}
declare type DropdownItem = BsPrefixRefForwardingComponent<typeof SafeAnchor, DropdownItemProps>;
declare const DropdownItem: DropdownItem;
export default DropdownItem;

Using the definition of React.MouseEventHandler,

type MouseEventHandler<T = Element> = EventHandler<MouseEvent<T>>;

you arrive to:

import React from 'react';
import { Dropdown } from 'react-bootstrap';
import { DropdownItemProps} from 'react-bootstrap/DropdownItem';

const handleClick = ( e: React.MouseEvent<DropdownItemProps>) => {
  e.preventDefault();
  // Do something
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Lesiak. Polish Developers rules. Dzięki za wytłumaczenie jak krowie na rowie. Next time when I will have similar problems I will know how to handle it. Thanks!
@Lesiak, may I ask you to have a look at a react-bootstrap related question here - stackoverflow.com/questions/64359611/… ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.