New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@airma/react-hooks-core

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@airma/react-hooks-core

This is a common react hook package

  • 18.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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 ...;
});

Keywords

FAQs

Package last updated on 29 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc