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

@cobuildlab/hooks-utils

v0.1.6

Published

Hooks with to help with ract applications

Downloads

61

Readme

Hooks utils

A set of hooks for common scenarios developing React and React Native applications.

Installation

  1. Run on your terminal the following command:
$ npm i --save @cobuildlab/hooks-utils
  1. To import the library anywhere you would like to use it:
import { usePromise } from '@cobuildlab/hooks-utils';

API Docs

| Object | Description | | ------------------------------------------------ | ---------------------------------------------------------- | | UsePromiseParams | Params for the usePromise. | | usePromise | A hook for resolve promises in a declarative way. | | useOnMount | Shorthand for useEffect with an empty dependencies array | | useOnClickOutside | Subscribe call to be called when a click is fired outside | usePrevious | Get previous value of a state |

UsePromiseParams

  • initialValue - An initial value for the hook.
  • reducer A function that mutates the state of the promise result before it gets returned.

usePromise(promise, initialValue)

  • It manages the promise lifecycle in a declarative way for React Components.

Example

// AgencyView.js

import { useCallback } from "react";
import { usePromise } from "@cobuildlab/hooks-utils";
import { fetchAgencies, fetchRoles } from "./agency-actions.js"

const AgencyView = ()=> {
  // NOTE: be aware that we using the same names for the error keys returned by the hook
  // This is just for keep the example simplest as posible
  const [agencies, loadingAgencies,  {error, call: fetchAgencies()} ] = usePromise(fetchAgencies);
  const [roles, loadingRoles, {error, call: fetchRoles()} ] = usePromise(()=>fetchRoles(agency), {
    onCmplete: (response)=>{
      console.log(response) // Roles response
    },
    onError: (error)=>{
      console.log(error) // Handle Error
    },
  });

  const fetchData = useCallback(() => {
    // Do something
    fetchRoles();
    fetchAgencies();
  });

  return ();
}

useOnMount(effectCallback)

Shorthand for useEffect with an empty dependencies array. It basically executes the function once on mount and unmount.

Example

import React from 'react';
import { useOnMount } from '@cobuildlab/hooks-utils';
import { useHistory } from 'react-router-dom';

const Session = () => {
  const history = useHistory();

  useOnMount(() => {
    if (!isNotAuthenticated) {
      history.push(LOGIN_PAGE_ROUTE);
    } else {
      fetchData(); // Do something
    }
  });

  return <React.Fragment>{children}</React.Fragment>;
};

useOnClickOutside(callback,innerRef)

Hook that subscribe a function to be call when a click is made out side the component on wich the ref is passed down.

import React, { useState, useRef } from 'react';
import { useOnClickOutside } from '@cobuildlab/hooks-utils';

const Dropdown = ({ children }) => {
  const [isOpen, setIsOpen] = useState(false);
  const closeDropdown = () => setIsOpen(false);
  const openDropdown = () => setIsOpen(true);

  // close dropdown when a click is fired outside itself
  const ref = useOnClickOutside(() => {
    closeDropdown();
  });
  // pass the ref returned by the hook to the component
  return isOpen && <div ref={ref}>{children}</div>;
};

If you already are using a ref inside your component yo could pass it to the hook so the hook use that instead of returning a ref

import React, { useState, useRef } from 'react';
import { useOnClickOutside } from '@cobuildlab/hooks-utils';

const Dropdown = ({ children }) => {
  const [isOpen, setIsOpen] = useState(false);
  const closeDropdown = () => setIsOpen(false);
  const openDropdown = () => setIsOpen(true);
  const ref = useRef();

  // Do somthing with the ref...

  // close dropdown when a click is fired outside itself
  useOnClickOutside(() => {
    closeDropdown();
  }, ref); // passing the ref to the hook

  // pass the same ref to the component
  return isOpen && <div ref={ref}>{children}</div>;
};

usePrevious(currentState)

Hook that returns the previous value of a state variable.

import React, { useState, useRef } from 'react';

const CreateUser = ({ children }) => {
  const [user, setUser] = useState({ username: '', password: '' });
  const previousUser = usePrevious(user);

  useEffect(() => {
    // compare user with previousUser state here
  }, [user, previousUser]);

  const onChange = (value: string, key: string): void => {
    setData({ ...user, [field]: value });
  }

  return <div onChange={onChange}>{children}</div>;
};

Changelog

v0.1.6

  • usePrevioushook

v0.1.5

  • useOnClickOutsidehook

v0.1.2

  • useOnMounthook

v0.1.0

  • usePromise hook