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

@airma/react-hooks-core

v18.3.0

Published

This is a common react hook package

Downloads

47

Readme

npm NPM downloads standard

@airma/react-hooks-core

It is a simple tool for providing some simple and useful react hooks for @airma/react-* tools. You can use it directly.

API

usePersistFn

export declare function usePersistFn<T extends (...args: any[]) => any>(
  callback: T
): T;

This hook is created for replacing useCallback, if you want to create a persist callback.

import {memo} from 'react';
import {usePersistFn} from '@airma/react-hooks-core';

const App = memo((props: {onChange: (value:string)=>void})=>{
    return ...;
});

const Layout = ()=>{
    const call = usePersistFn((v: string)=>{});

    return <App onChange={call}/>
}

useMount

export declare function useMount(callback: () => (() => void) | void): void;

This hook is created for listen the mount effect of functional component.

useUpdate

export declare function useUpdate<T extends any[]>(
  callback: (prevDeps: typeof deps) => (() => void) | void,
  deps?: T
): void;

This hook is created for listen the update effect of functional component.

import {memo, useState} from 'react';
import {useUpdate} from '@airma/react-hooks-core';

const App = memo((props: {value:number})=>{

    const [count, setCount] = useState(props.value);

    useUpdate((prev)=>{
        if(!prev){
            return;
        }
        const [prevValue] = prev;
        console.log(prevValue)
    }, [props.value]);

    return ...;
});

useRefresh

export declare function useRefresh<T extends (...args: any[]) => any>(
  method: T,
  params:
    | Parameters<T>
    | {
        refreshDeps?: any[];
        variables: Parameters<T>;
      }
): void;

This hook helps you call a function when the parameters have been changed.

import {useEffect, useState} from 'react';
import {useRefresh} from '@airma/react-hooks-core';

function useIntervalCountDown() {
    const [count, setCount] = useState(60);
    const [s, setS] = useState(count);
    useEffect(()=>{
        const id = window.setInterval(()=>{
            setCount(c=>c-1);
        },1000);
        return ()=> {
            window.clearInterval(id);
        }
    },[]);
    
    useRefresh((seconds)=>setS(seconds<0?0:seconds), [count]);
    
    return s+'s';
}

useUnmount

export declare function useUnmount(destroy: () => void): void;

This hook is created for listen the unmount effect of functional component.

Recommends

We recommend you use these packages for a better experience.

  1. @airma/react-hooks: It provides simple APIs: usePersistFn, useMount, useUpdate for usage.
  2. @airma/react-state: It provides a model hook usage for manage react states.
  3. @airma/react-effect: It provides asynchronous state manage hooks for easy usage.

Examples

@airma/react-hooks

import { useUpdate } from '@airma/react-hooks';
import type { User } from './type';

export function useUserUpdateReload(user: User){
    const { orders, messages } = user;
    useUpdate(([prevOrders, prevMessages])=>{
        if (
            orders.length !== prevOrders.length ||
            messages.length !== prevMessages.length
        ) {
            window.location.reload();
        }
    }, [orders, messages]);
}

@airma/react-state

import {memo} from 'react';
import {useModel} from '@airma/react-state';

const Layout = memo(({interval}:{interval: number})=>{
    const instance = useModel((state: number)=>{
        return {
            value: state,
            isNegative: state < 0,
            increase(): number{
                return state + interval;
            },
            decrease(): number{
                return state - interval;
            }
        }
    }, 0);
    
    const {
        value,
        isNegative,
        increase,
        decrease
    } = instance;

    return ......;
});

@airma/react-effect

import {memo} from 'react';
import {useQuery} from '@airma/react-effect';
import {fetchUser} from './session';

const App = memo(({userId}:{userId: number})=>{
    const [
        {
            data: user,
            isFetching,
            loaded,
            error
        },
        trigger
    ] = useQuery(fetchUser, [ userId ]);
    
    const refetch = ()=>{
        trigger(userId);
    };
    
    return ...;
});