Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@wethegit/react-hooks

Package Overview
Dependencies
Maintainers
3
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wethegit/react-hooks

A collection of helpers for use in React projects.

  • 1.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
231
decreased by-1.28%
Maintainers
3
Weekly downloads
 
Created
Source

react-hooks

A collection of helpers for use in React projects.

Jump to a section:

  • useAsync
  • useInView
  • useLocalStorage
  • usePreventScroll
  • useUserPrefs

Installation

Install from the NPM registry using your package manager of choice.

npm install @wethegit/react-hooks

Hooks

useAsync

Manages the calling of an asynchronous JavaScript function, while providing the return data and the state of the function call.
useAsync(asyncFn, [deferred])

Arguments
ArgumentsTypeDescription
asyncFnFunctionA JavaScript function which returns a Promise.
deferredBooleanOptional. Whether to defer the execution of asyncFn. Default: false.
Return value

useAsync returns an Object containing the following properties:

PropertyTypeDescription
data-The data returned from the resolved Promise, as a result of calling asyncFn.
statusStringOne of: "idle", "pending", "success", or "error".
errorStringThe error, if applicable, as thrown by asyncFn.
runFunctionThe function to invoke at a later time, which calls asyncFn. This will be null if the deferred argument was passed as false.
Usage

Instant invocation of the provided async function:

import { useAsync } from "@wethegit/react-hooks"

const MyComponent = () => {
  const { data, status, error } = useAsync(() =>
    fetch("https://my-cool-api.com/some-endpoint")
  );

  console.log(data, status, error)
}

Deferred invocation of the provided async function:

import { useAsync } from "@wethegit/react-hooks"

const MyComponent = () => {
  const { run, data, status, error } = useAsync(() =>
    fetch("https://my-cool-api.com/some-endpoint"),
    true
  )

  const getMyData = () => {
    if (data) return
    run()
  }

  console.log(data, status, error)

  return (
    <button onClick={getMyData}>Get data</button>
  )
}

useInView

Detects whether a DOM element is in the viewport, using the IntersectionObserver API.
useInView([threshold], [once], [setInViewIfScrolledPast])

Arguments
ArgumentsTypeDescription
thresholdFloatDefault: 0.3. A value between 0 and 1, which maps to a percentage of the DOM element's height. Once this amount of the DOM element is within the viewport, the hook will consider the element "in view". This value is directly passed to an IntersectionObserver, so for more details on what this argument means, check out the IntersectionObserver docs on MDN.
onceBooleanDefault: true. Whether to detach the observer from the DOM element after the first intersection callback is invoked.
setInViewIfScrolledPastBooleanDefault: true. Whether to consider the element already "in-view", if the top of it is already scrolled beyond the bounds of the viewport when this hook is called.
Return value

useInView returns an Array containing the following values:

Return valueTypeDescription
setTargetRefFunctionPass this function to the ref prop of the DOM element you want to track visibility of.
isIntersectingBooleanWhether the target DOM element is in view, based on the provided threshold argument.
targetRefReact RefThe DOM node itself, once set by the setTargetRef function.
Usage

Toggling a className on and off, based on whether a <section> is in view:

import { useInView } from "@wethegit/react-hooks"

const IN_VIEW_CLASSNAME = "in-view"

const MyComponent = () => {
  const [setSectionRef, sectionInView, section] = useInView()

  return (
    <>
      <section ref={setSectionRef} className={sectionInView ? "in-view" : ""}>
        <p>Sit quis ut id sit anim. Enim qui est ut tempor sunt.</p>
      </section>
    </>
  )
}

useLocalStorage

Reads and writes a piece of state to the browser's local storage.
useLocalStorage(key, [defaultValue])

Arguments
ArgumentsTypeDescription
keyStringThe name of the property you wish to save to localStorage.
defaultValueStringDefault: "". The default / initial value to set to the localStorage key.
Return Value

useLocalStorage returns an Array containing the following values:

ArgumentsTypeDescription
stateStringThe current value of the localStorage key in the browser.
setStateFunctionState setter function, used to update the localStorage value in the browser.
Usage

Setting a localStorage value for a user's favorite fruit, based on their selection:

import { useLocalStorage } from "@wethegit/react-hooks"

const FRUITS = ["Apple", "Banana", "Orange"]

const FruitSelector = () => {
  const [fruit, setFruit] = useLocalStorage("fruit")

  return (
    <>
      <form>
        <fieldset>
          <legend>Select your favorite fruit</legend>
          <ul>
            {FRUITS.map((fruit) => (
              <li key={fruit}>
                <input
                  type="radio" name="fruit" id={fruit} value={fruit}
                  onChange={() => setFruit(fruit)}
                />
                <label for={fruit}>{fruit}</label>
              </li>
            ))}
          </ul>
        </fieldset>
      </form>

      {fruit && <p>Your favorite fruit is {fruit}</p>}
    </>
  )
}

usePreventScroll

Toggles the overflow: hidden CSS declaration on the <body> DOM element.
usePreventScroll(state)

Arguments
ArgumentsTypeDescription
stateBooleanWhether to prevent scrolling on the <body> element.
Usage

Toggling scrollability of the <body> element, based on whether a modal window is displayed:

import { usePreventScroll } from "@wethegit/react-hooks"
import { useState } from "react"

const MyComponent = () => {
  const [modalIsOpen, setModalIsOpen] = useState(false)

  usePreventScroll(modalIsOpen)

  return (
    <>
      <button onClick={() => setModalIsOpen(state => !state)}

      {modalIsOpen && (
        <div>How about this amazing modal window, eh?</div>
      )}
    </>
  )
}

useUserPrefs

Provides access to various user preferences, the values of which are stored in the browser's localStorage.
useUserPrefs()

Return value

useUserPrefs returns an Object containing the following properties:

PropertyTypeDescription
prefersDarkColorSchemeBooleanWhether the user has either turned on "prefers dark color scheme" in their OS-level settings, or has chosen the option exposed by your site via some UI.
togglePrefersDarkColorSchemeFunctionAccepts a single argument (Boolean) which toggles the localStorage state of prefersDarkColorScheme.
prefersReducedDataBooleanWhether the user has either turned on "prefers reduced data" in their OS-level settings, or has chosen the option exposed by your site via some UI.
togglePrefersReducedDataFunctionAccepts a single argument (Boolean) which toggles the localStorage state of prefersReducedData.
prefersReducedMotionBooleanWhether the user has either turned on "prefers reduced motion" in their OS-level settings, or has chosen the option exposed by your site via some UI.
togglePrefersReducedMotionFunctionAccepts a single property (Boolean) which toggles the localStorage state of prefersReducedMotion.
Usage

Passing a dynamic animation duration prop (in milliseconds) to an imaginary <Menu> component. The duration will change based on the state of prefersReducedMotion:

import { useUserPrefs } from "@wethegit/react-hooks"
import Menu from "../somewhere"

const MyComponent = () => {
  const { prefersReducedMotion } = useUserPrefs()

  return (
    <Menu animationDuration={prefersReducedMotion ? 1200 : 0} />
  )
}

Toggling various color props based on whther the user prefers "dark mode", on an imaginary <Menu> component:

import { useUserPrefs } from "@wethegit/react-hooks"
import Menu from "../somewhere"

const THEMES = {
  dark: {
    color: "#fff",
    background: "#000",
  },
  light: {
    color: "#000",
    background: "#fff",
  }
}

const MyComponent = () => {
  const { prefersDarkColorScheme } = useUserPrefs()

  const theme = THEMES[prefersDarkColorScheme ? "dark" : "light"]

  return (
    <Menu color={theme.color} background={theme.background} />
  )
}

Keywords

FAQs

Package last updated on 20 May 2023

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