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

@avidian/hooks

v2.1.0

Published

React helper hooks for convenience

Downloads

3

Readme

@avidian/hooks

React helper hooks for convenience

Installation

npm

npm install @avidian/hooks

yarn

yarn add @avidian/hooks

Usage

useURL

Used to generate route path relative to the current component and route. (react-router-dom or react-router-native)

Example (using react-router-dom)

import React, { FC } from 'react';
import { Switch, Route } from 'react-router-dom';
import { useURL } from '@avidian/hooks';
import Form from './Form';
import List from './List';

export default function Component(props) => {
    const url = useURL();

    return (
        <Switch>
            <Route path={url('')} exact component={List} />
            <Route path={url('/add')} component={Form} />
            <Route path={url('/:id/edit')} component={Form} />
        </Switch>
   );
};

useMode

Used for determining form mode (Add or Edit). Defaults to 'Add'

import React from 'react';
import { useMode } from '@avidian/hooks';

export default function Form(props) {
    const [mode, setMode] = useMode();
    const match = useRouteMatch<{ id: string }>();

    const id = match.params.id;

    const fetchData = async (id) => {
        //
    };

    useEffect(() => {
        if (match.path.includes('edit')) {
            setMode('Edit');
            fetchData(id);
        }
    }, []);

    return (
        <div>
            {mode} Data
            <form>
                <input />
                <button type='submit' >submit</button>
            </form>
        </div>
    );
}

useNullable

Define state that can be null.

import React from 'react';
import { useNullable } from '@avidian/hooks';

export default function Component() {
    const [numberOrNull, setNumberOrNull] = useNullable(15);

    return (
        <button onClick={() => {
            // errors if you're using typescript
            setNumberOrNull('1');
            setNumberOrNull({});
            setNumberOrNull();

            // works
            setNumberOrNull(0);
            setNumberOrNull(null);
        }}>
            click me
        </button>
    );
}

useArray

Define an array as state.

import React from 'react';
import { useArray } from '@avidian/hooks';

export default function Component() {
    // defaults to an array instead of undefined
    const [array, setArray] = useArray();

    // ...
}

useArrayComplex

Define an array as state with it's helper methods. Using the helper methods will automatically mutate the array state for you.

import React from 'react';
import { useComplexArray } from '@avidian/hooks';

export default function Component() {
    const { array, set, push, filter, update, remove, clear } = useComplexArray();

    // ...
}

usePartial

It's the same as useState({}) but it's useful in typescript, it makes the properties of the object optional with the Partial<T> generic.

import React from 'react';

// required type
type Data = {
    title: string;
    description: string;
}

export default function Component() {
    const [value, setValue] = usePartial<Data>({
        // passing an object is optional
        title: 'My Title',
    });

    // ...
}

useToggle

Define a toggleable boolean state.

import React from 'react';
import { useToggle } from '@avidian/hooks';

export default function Component() {
    const [value, toggleValue] = useToggle(false);

    // explicitly set value
    toggleValue(true);

    // toggle the value
    toggleValue();
}

useTimeout

Define a callback with a timeout

import React from 'react';
import { useTimeout } from '@avidian/hooks';

export default function Component() {
    const { reset, clear } = useTimeout(() => {
        // do something
    }, 1000)    
}

useDebounce

Define a callback with a debounce.

import React from 'react';
import { useDebounce } from '@avidian/hooks';

export default function Component() {
    useDebounce(() => {
        // do something
    }, 5000, []);
}

useUpdateEffect

Same as useEffect but executes after update.

import React from 'react';
import { useUpdateEffect } from '@avidian/hooks';

export default function Component() {
    useUpdateEffect(() => {
        // do something after render
    }, []);
}

usePrevious

Stores the previous value when updated.

import React, { useState } from 'react';
import { usePrevious } from '@avidian/hooks';

export default function Component() {
    const [count, setCount] = useState(0);
    const value = usePrevious(count);

    // `value` will be 0 after `count` is updated to `1`
    setCount(1);
}

useLocalStorage and useSessionStorage

Define state that maps to a storage. remove sets the value to undefined.

import React from 'react';
import { useLocalStorage, useSessionStorage, useStorage } from '@avidian/hooks';

export default function Component() {
    const [value, setValue, remove] = useLocalStorage('name', 'Joe');
    const [value, setValue, remove] = useSessionStorage('name', 'Joe');

    // custom storage
    const storage = new ClassThatImplementsStorageInterface();
    const [value, setValue, remove] = useStorage('name', 'Joe', storage);
}

License

This library is open-sourced software licensed under the MIT license.