npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rely-use-callback

v0.0.10

Published

Rely on useCallback in react app

Downloads

13

Readme

rely-use-callback

Rely on typed useCallback in your ReactApp 🥂

npm types License

✨ Typed useCallback

Use type RelyCallback to have type guard for memo components and hooks dependencies

🤖 Motivation

Sometimes you need to check if a function exact is created by useCallback.

For example, this is may necessary for functions that are passed depending on other hooks

import { useCallback, memo } from 'react';

/** 
 * Imagine that we have a hook that call some business logic in useEffect
 */
const useSomethinkToDoOnEffect = (todoFunction: () => void) => {
    useEffect(() => {
        todoFunction()
    }, [
        /** 
         * {@link todoFunction} passed to hook dependencies,
         * so it is may important to save function 
         * reference by {@link useCallback} to keep business logic
         */
        todoFunction
    ]);
}

/**
 * Also we can use {@link memo} HOC, where also important
 * pass function with save reference by {@link useCallback}
 */
const MemoizedComponent: FC<{ onClick: () => void }> = memo(({
    onClick
}) => <ChildComponent />)

const App = () => {
    /** 
     * But currently TS types of {@link useSomethinkToDoOnEffect} 
     * can't warn us to use function created by {@link useCallback}
     * 
     * So we can use both variants without ts errors
     */
    
    useSomethinkToDoOnEffect(() => {});
    
    /** OR */
    
    const functionWithCallback = useCallback(() => {}, []);
    useSomethinkToDoOnEffect(functionWithoutCallback);

    return (
        <MemoizedComponent 
            /** nothing ts errors */
            onClick={() => {}}
        />
    );
}

😲 You probably don't need use this package

I think, this code matches for you at 99 %, but up to you

import { DependencyList, useCallback as _useCallback } from "react";

interface UseCallback<T extends Function> {}

const useCallback = <T extends Function>(
    handler: T,
    deps: DependencyList
): UseCallback<T> => _useCallback(handler, deps);

📦 Install & Usage

1️⃣ Install by running: npm i rely-use-callback --save

2️⃣ Replace a useCallback from react with a useCallback from rely-use-callback:

- import { useCallback } from 'react';
+ import { useCallback, RelyCallback } from 'rely-use-callback';

const useSomethinkToDoOnEffect = (  
-    todoFunction: () => void
+    todoFunction: RelyCallback<() => void>
) => {  
    useEffect(() => {  
        todoFunction()  
    }, [ todoFunction ]);  
}

const MemoizedComponent:FC<{  
-    onClick: RelyCallback<() => void>
+    onClick: () => void 
}> = memo(() => <ChildComponent onClick={onClick} />)

When you use useCallback hook from rely-use-callback, you can use type RelyCallback and save type guard on these cases and rely on callback function in your ReactApp

💪💪💪 Extra more productive way

A more productive way is to use typed useCallback in conjunction with eslint-plugin-react-hooks

More about this eslint rules you can read on plugin page

{
  "rules": {
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "warn"
  }
}

🥂 License

MIT