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

react-hookedup

v0.1.2

Published

A collection of useful React hooks

Downloads

2,890

Readme

react hookedup

npm GitHub license Storybook

Hooks require at least React 16.8

Installation

using npm

npm install react-hookedup --save

using yarn

yarn add react-hookedup

Demo

Visit here

hooks

common hooks

| Name | Description | Arguments | Returns | | ---------------------------------------- | --------------------- | ----------------- | ----------- | | useArray | useful hook for manipulating arrays | initial value | {value, setValue, removeById, removeIndex, clear} | | useBoolean | useful hook for manipulating booleans | initial value | {value, setValue, toggle, setTrue, setFalse} | | useCounter | counter hook | value,{upperLimit,lowerLimit,step,loop} | {value, setValue, increase,decrease} | | useFocus | focus hook | null | {focused, bind} | | useHover | hover hook | null | {hovered, bind} | | useInput | input handling hook | initial value | {value, setValue, onChange, bindToInput, bind, hasValue, clear} |

lifecycles hooks

| Name | Description | Arguments | Returns | | ---------------------------------------- | --------------------- | ----------------- | ----------- | | useLifecycleHooks | use lifecycle methods | {onMount, onUnmount} | void | | useOnMount | componentDidMount like lifecycle | Function | void | | useOnUnmount | componentWillUnmount like lifecycle | Function | void | | useMergeState | merge the previous state with new one | initial value of the state | {setState: Function, state} | | usePrevious | get the previous value of the state | initial value of the state | the previous value |

timers hooks

| Name | Description | Arguments | Returns | | ---------------------------------------- | --------------------- | ----------------- | ----------- | | useInterval | use setInterval via hooks | {function, delay} | void | | useTimeout | use setTimeout via hooks | {function, delay} | void |

network hooks

| Name | Description | Arguments | Returns | | ---------------------------------------- | --------------------- | ----------------- | ----------- | | useOnLineStatus | check if the browser is connected to the internet | null| {online} |

Usage

useArray

import { useArray } from 'react-hookedup';

const ExampleUseArray = () => {
  const {
    add,
    clear,
    removeIndex,
    value: currentArray
  } = useArray(['cat','dog','bird']);

  const {
    bindToInput,
    value
  } = useInput('');

  const {
    bindToInput: bindToInput2,
    value: value2
  } = useInput('');

  return(
    <div>
      <p>
        current array is : {JSON.stringify(currentArray)}
      </p>
      <div>
        add element :
        <input {...bindToInput}  />
        <button onClick={() => add(value)}>add</button>
      </div>
      <div>
        remove element by index :
        <input {...bindToInput2}  />
        <button onClick={() => removeIndex(value2)}>delete</button>
      </div>
      <div>
        delete all items :
        <button onClick={() => clear()}>clear</button>
      </div>
    </div>
  )
};

useBoolean

import { useBoolean } from 'react-hookedup';

const ExampleUseBoolean = () => {
  const {toggle, value} = useBoolean(false);

  return(
    <div>
      <p>{JSON.stringify(value)}</p>
      <button onClick={() => toggle()}>toggle</button>
    </div>
  );
};

Methods:

  • toggle
  • setTrue
  • setFalse

useOnMount

import { useOnMount } from 'react-hookedup';

const App = () => {
  useOnMount(() => console.log("hello world"));
  return <div> hello world </div>;
};

useOnUnmount

const App = () => {
  useOnUnmount(() => console.log("goodbye world"));
  return <div> goodbye world </div>;
};

useLifecycleHooks

const App = () => {
  useLifecycleHooks({
    onMount: () => console.log("mounted!"),
    onUnmount: () => console.log("unmounting!")
  });

  return <div> hello world </div>;
};

useCounter

const counter = useCounter(0);
const limitedNumber = useCounter(3, { upperLimit: 5, lowerLimit: 3 });
const rotatingNumber = useCounter(0, {
  upperLimit: 5,
  lowerLimit: 0,
  loop: true
});

Methods:

Both increase and decrease take an optional amount argument which is 1 by default, and will override the step property if it's used in the options.

  • increase(amount = 1)
  • decrease(amount = 1 )

Options:

  • lowerLimit
  • upperLimit
  • loop
  • step - sets the increase/decrease amount of the number upfront, but it can still be overriden by number.increase(3) or number.decrease(5)

useInput

const newTodo = useInput("");
<input value={newTodo.value} onChange={newTodo.onChange} />
<input {...newTodo.bindToInput} />
<Slider {...newTodo.bind} />

Methods:

  • clear
  • onChange
  • bindToInput - binds the value and onChange props to an input that has e.target.value
  • bind - binds the value and onChange props to an input that's using only e in onChange (like most external components)

Properties:

  • hasValue

useFocus

const ExampleUseFocus = () => {
  const {focused, bind} = useFocus();

  return(
    <div>
      <p>this is input is : {focused ? 'focused' : 'not focused'}</p>
      <input {...bind}  />
    </div>
  );
};

useHover

const ExampleUseHover = () => {
  const {hovered, bind} = useHover();

  return(
    <div>
      <p>this is input is : {hovered ? 'hovered' : 'not hovered'}</p>
      <input {...bind}  />
    </div>
  );
};

useArray

const todos = useArray([]);

Methods:

  • add
  • clear
  • removeIndex
  • removeById

useMergeState

const { state, setState } = useMergeState({ loading: false });
setState({ loading: true, data: [1, 2, 3] });

Methods:

  • setState(value) - will merge the value with the current state (like this.setState works in React)

Properties:

  • state - the current state

usePrevious

Use it to get the previous value of a prop or a state value.
It's from the official React Docs.
It might come out of the box in the future.

const Counter = () => {
  const [count, setCount] = useState(0);
  const prevCount = usePrevious(count);
  return (
    <h1>
      Now: {count}, before: {prevCount}
    </h1>
  );
};

useInterval

const useIntervalExample = () => {
  useInterval(() => alert('hello world'), 1500);

  return (
    <h1>
      I will alert in 1.5 s
    </h1>
  );
};

useTimeout

const useTimeoutExample = () => {
  useTimeout(() => alert('hello world'), 1500);

  return (
    <h1>
      I will alert in 1.5 s
    </h1>
  );
};

useOnLineStatus

const useOnLineStatusExample = () => {
  const {online} = useOnLineStatus();

  return (
    <h1>
      you are : {online ? 'online' : 'offline'}
    </h1>
  );
};