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

@shopify/react-hooks

Package Overview
Dependencies
Maintainers
13
Versions
98
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@shopify/react-hooks

A collection of primitive React hooks.

  • 1.2.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
108K
decreased by-53.98%
Maintainers
13
Weekly downloads
 
Created
Source

@shopify/react-hooks

Build Status License: MIT npm version npm bundle size (minified + gzip)

A collection of primitive React hooks.

Installation

$ yarn add @shopify/react-hooks

Usage

useOnValueChange()

This hook will track a given value and invoke a callback when it has changed.

function MyComponent({foo}: {foo: string}) {
  useOnValueChange(foo, (newValue, oldValue) => {
    console.log(`foo changed from ${oldValue} to ${newValue}`);
  });

  return null;
}

useTimeout()

This hook provides a declarative version of setTimeout(). The first argument is a callback that will be invoked after the given delay (number of milliseconds) as the second argument.

function MyComponent() {
  const [foo, setFoo] = React.useState('Bar');

  useTimeout(() => setFoo('Baz!'), 5000);

  return <div>{foo}</div>;
}

useLazyRef()

This hook creates a ref object like React’s useRef, but instead of providing it the value directly, you provide a function that returns the value. The first time the hook is run, it will call the function and use the returned value as the initial ref.current value. Afterwards, the function is never invoked. You can use this for creating refs to values that are expensive to initialize.

function MyComponent() {
  const ref = useLazyRef(() => someExpensiveOperation());

  React.useEffect(() => {
    console.log('Initialized expensive ref', ref.current);
  });

  return null;
}

useMountedRef()

This hook keep track of a component's mounted / un-mounted status and returns a ref object like React’s useRef with a boolean value representing said status. This is often use when a component contains async task that set state after the task resolved.

import React from 'react';
import {useMountedRef} from '@shopify/react-hooks';

function MockComponent() {
  const [result, setResult] = React.useState();
  const mounted = useMountedRef();

  async function handleOnClick() {
    const result = await fetchData();

    if (mounted.current) {
      setData(result);
    }
  }

  return (
    <button onClick={handleOnClick} type="button">
      Fetch Data
    </button>
  );
}

FAQs

Package last updated on 17 Oct 2019

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