loktar00 / react-lazy-load

React component that renders children elements when they enter the viewport.
MIT License
979 stars 166 forks source link

Typescript react not supported? #126

Closed luojinghui closed 2 years ago

luojinghui commented 6 years ago

Typescript react not supported?

ghost commented 5 years ago

Works for me.

sunknudsen commented 4 years ago

@george-dotdev What type definitions are you using?

ghost commented 4 years ago

@sunknudsen Hmm actually I'm not sure. I think I made a mistake when I commented on here. Sorry

JohannesMerkt commented 4 years ago

I added this type definition to my project and it works:

declare module "react-lazy-load" { import { Component, ReactNode } from "react";

export interface LazyLoadProps {
    className?: string;
    height?: number | string;
    width?: number | string;
    debounce?: boolean;
    elementType?: string;
    offset?: number;
    offsetBottom?: number;
    offsetHorizontal?: number;
    offsetLeft?: number;
    offsetRight?: number;
    offsetTop?: number;
    offsetVertical?: number;
    threshold?: number;
    children?: ReactNode;
    throttle?: number | boolean;
    onContentVisible?: Function;
}

export default class LazyLoad extends Component<LazyLoadProps> {
    constructor(props: LazyLoad);
}

}

And sorry code formating in the comment didnt work somehow

grafluxe commented 4 years ago

Thanks for sharing the type declarations @JohannesMerkt. I made some updates to your logic:

  1. Replaced onContentVisible?: Function; with onContentVisible?: () => void;
  2. Replaced the class with a React function component
declare module "react-lazy-load" {
  import { FC, ReactNode } from "react";

  interface Props {
    className?: string;
    height?: number | string;
    width?: number | string;
    debounce?: boolean;
    elementType?: string;
    offset?: number;
    offsetBottom?: number;
    offsetHorizontal?: number;
    offsetLeft?: number;
    offsetRight?: number;
    offsetTop?: number;
    offsetVertical?: number;
    threshold?: number;
    children?: ReactNode;
    throttle?: number | boolean;
    onContentVisible?: () => void;
  }

  const LazyLoad: FC<Props> = (props) => {};

  export default LazyLoad;
}
JohannesMerkt commented 4 years ago

@grafluxe aweseome! Thanks for sharing it.

loktar00 commented 2 years ago

Hah I just noticed this after working on converting this library to TS, fortunately the types were pretty straight forward, exactly what I came up with as well.

https://github.com/loktar00/react-lazy-load/tree/feature/update-to-18

loktar00 commented 2 years ago

Should be fixed with 4.0 and #155