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

@npm.piece/utils

v1.0.150

Published

Frequently used tools for React

Downloads

38

Readme

Frequently used hooks for React

Install

npm i @npm.piece/utils
yarn add @npm.piece/utils

useDebounce (with lodash)

import { useDebounce } from 'libs/utils/dist/index'
const log = useDebounce((params) => console.log(params), 1000);

log("123")

useThrottle (with lodash)

import { useThrottle } from '@npm.piece/utils'
const log = useThrottle((params) => console.log(params), 1000);

log("123")

usePortraitDetect

import { usePortraitDetect } from '@npm.piece/utils'
const isPortrait = usePortraitDetect()

useOnlineDetect

import { useOnlineDetect } from '@npm.piece/utils'
const isOnline = useOnlineDetect()

useSizeDetect

import { useSizeDetect } from '@npm.piece/utils'
const {
  clientHeight,
  clientWidth,
  innerHeight,
  innerWidth
} = useSizeDetect()

useKeyPressDetect

import { useKeyPressDetect } from '@npm.piece/utils'
const isKeyFPressed = useKeyPressDetect("f")

useClickOutside

import { useClickOutside } from '@npm.piece/utils'
const ref = useRef()
useClickOutside(ref, () => {
})

useOnKeyPress

import { useOnKeyPress } from '@npm.piece/utils'
const callback = useCallback(() => {
  // i use toLowerCase() in code, so it doesn't matter, if you use 'Enter' or 'enter'
  console.log('npm.piece')
}, [])

useOnKeyPress(callback, 'Enter')

useInterval

import { useInterval } from '@npm.piece/utils'
useInterval(() => {
}, 1000);

useTimeout

import { useTimeout } from '@npm.piece/utils'
useTimeout(() => {
}, 1000);

useFocus

import { useFocus } from '@npm.piece/utils'
const checkViewPortRef = useRef < HTMLDivElement > (null);
const setFocus = useFocus(ref)

useEffect(() => {
  setFocus()
}, [])

return <input ref={htmlElRef} />

useIsVisibleElement

import { useIsVisibleElement } from '@npm.piece/utils'
const checkViewPortRef = useRef < HTMLDivElement > (null);
const isInViewPort = useIntersection(checkViewPortRef);

return <div ref={checkViewPortRef} />

useEffectWithoutFirstCall

import { useEffectWithoutFirstCall } from '@npm.piece/utils'
useEffectWithoutFirstCall(() => {
}, []);

CustomServiceInjector

The Service Injector component is designed to inject custom hooks containing useEffect, to your application without calling re-render of child components.

import { ServiceInjector } from '@npm.piece/utils'
<ServiceInjector
  services={[condition.service, mobile.service]}
/>

ErrorBoundary

import { ErrorBoundary } from '@npm.piece/utils'
<ErrorBoundary errorComponent={<h1>error</h1>}>
    <div/>
</ErrorBoundary>

ArrayRender

This component is a generic component for displaying an array of elements. Instead of just using map to convert an array of elements into JSX elements, the ArrayRender component takes an array of items and a renderItem function and processes them internally. The main purpose of this component is to simplify repetitive code when displaying a list of items and keep the code clean and modular.

import { ArrayRender } from '@npm.piece/utils'
<ArrayRender items={items} renderItem={(item) => <itemTemplate key />} />

deepClone

import { deepClone } from '@npm.piece/utils'
const newObj = deepClone({ name: 'npm.piece' })

MergeObjects

import { mergeObjects } from '@npm.piece/utils'
const a = {
  name: 'npm.piece',
  location: {
    city: 'City 17'
  }
}

const b = {
  age: 18,
  location: {
    flat: 'H15'
  }
}

const c = {
  price: 100
}


const d = mergeObjects(a, b, c)
// d will be:
const d = {
  name: 'npm.piece',
  age: 18,
  price: 100,
  location: {
    city: 'City 17',
    flat: 'H15'
  }
}

setToSessionStorage / setToLocalStorage / getFromSessionStorage / getFromLocalStorage

import {
  setToSessionStorage,
  setToLocalStorage,
  getFromSessionStorage,
  getFromLocalStorage
} from '@npm.piece/utils'
  setToSessionStorage('token', { age: 100 })
  setToLocalStorage('token', { age: 100 }),
  getFromSessionStorage('token'),
  getFromLocalStorage('token')

Micro-Frontend Events

moved to event-bus

IndexedDB (with idb)

import { IndexedDB } from '@npm.piece/utils'
useEffect(() => {
  const runIndexDb = async () => {
    const idb = new IndexedDB('test')
    //if you need to delete database, add new version number for second argument
    await idb.createObjectStore(['languages', 'students'], 1)
    await idb.putValue('languages', { name: 'JavaScript' })
    await idb.putBulkValue('languages', [
      { name: 'TypeScript' },
      { name: 'C#' }
    ])
    await idb.getValue('languages', 1)
    await idb.getAllValue('languages')
    await idb.deleteValue('languages', 1)
  }
  runIndexDb()
}, [])

createQueryParams

import { createQueryParams } from '@npm.piece/utils'
createQueryParams({ page: 1, size:10 })

useQueryParams

import { useQueryParams } from '@npm.piece/utils'
const params = useQueryParams()