A
A
Asker18882020-05-07 12:55:51
typescript
Asker1888, 2020-05-07 12:55:51

How to extend or overwrite a third party library component interface property?

Good afternoon!
Installed the Link component from the react-scroll package .

I need to hang onClick on this component with a handler that handles the event, but in the component's interface, onClick does not involve passing the event.
How can the interface be extended without changing typing.d.ts within the package itself?

How I try to use:

<ScrollLink 
   to="program" 
   duration={300} 
   smooth={true} 
   offset={-50} 
   className="Link Header-Link" 
   data-ymgoal="MY_GOAO"
   onClick={ ymHandler }
  >


Interface:
export interface ReactScrollLinkProps {
    to: string;
    containerId?: string;
    activeClass?: string;
    spy?: boolean;
    hashSpy?: boolean;
    smooth?: boolean | string;
    offset?: number;
    delay?: number;
    isDynamic?: boolean;
    onClick?(): void;
    duration?: number | string;
    absolute?: boolean;
    onSetActive?(to: string): void;
    onSetInactive?(): void;
    ignoreCancelEvents?: boolean;
}


My handler function:
const ymHandler = (event: any): void => {
    console.log(event);
    const goal = event.currentTarget.getAttribute('data-ymgoal');
    ym('reachGoal', goal);
  }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shsv382, 2020-05-07
@shsv382

You can wrap it in a div and hang an onClick handler on it by changing

const goal = event.currentTarget.getAttribute('data-ymgoal');

on the
const goal = event.currentTarget.firstElementChild.getAttribute('data-ymgoal');

F
forspamonly2, 2020-05-08
@forspamonly2

fix typing and send a pull request to DefinitelyTyped .
because react-scroll itself passes the event .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question