1.0.9 • Published 4 months ago

react-generic-functions v1.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

react-generic-functions

Simple generic memo & forwardRef react functions

Usage

import { memo, forwardRef } from 'react-generic-funtions';

interface ExampleComponentProps<T> {
    genericProp: T
}

const ExampleComponent = forwardRef(
        <T extends string | number>({ genericProp }: ExampleComponentProps<T>,
        ref: ForwardedRef<HtmlDivElement>
    ) => {
        const message = useMemo(() => {
            if (typeof genericProp === 'number') {
                return 'hello';
            } else {
                return 'word';
            }
        }, [genericProp])

        return (
            <div ref={ref}>
                {message}
            </div>
        );
    }
);

import { useRef } from 'react';

const ExampleRoot: FC = () => {
    const componentRef = useRef<HtmlDivElement>(null);
    const [state, setState] = useState<string | number>(0)
    
    // ...

    return (
        <div>
            {/* ... */}
            <ExampleComponent genericComponent={state} ref={ref} />
            {/* ... */}
        </div>
    )
}
1.0.9

4 months ago

1.0.8

4 months ago

1.0.7

4 months ago

1.0.6

4 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.0

5 months ago