🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

@nrk/nrkno-sanity-react-utils

Package Overview
Dependencies
Maintainers
144
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nrk/nrkno-sanity-react-utils

Various React related utility functions and libs used by nrkno-sanity.

latest
Source
npmnpm
Version
1.1.1
Version published
Weekly downloads
36
200%
Maintainers
144
Weekly downloads
 
Created
Source

@nrk/nrkno-sanity-react-utils

Various React related utility functions and libs used by nrkno-sanity.

Installation

npm install --save @nrk/nrkno-sanity-react-utils

Usage

useSetRefs

Callback that sets all provided refs. Useful when you need multiple reference handles to the same component.

For instance, a ref for your local state and for forwarded Sanity ref in custom input component.

function SomeComponent(props: any, forwardRef: ForwardedRef<HTMLDivElement>) {
  const ref = useRef<HTMLDivElement | null>(null)
  const setRefs = useSetRefs(ref, forwardRef);
  return <div ref={setRefs} />;
}

useResizeObserver

Get a callback whenever an observed element changes size.

function SomeComponent() {
 //useState and NOT useRef
  const [ref, setRef] = useState<HTMLDivElement | null>(null);

  const onResize= useCallback((domRect: DOMRect) => {
      // invoked whenever the div is resized
    }, [])
    
  useResizeObserver(onResize, ref);
  return <div ref={setRef} />;
}

useDebouncedEffect

Takes an effect callback and dependency array (same arguments as React.useEffect), and a debounce delay.

When dependencies change (including component mount, ala useEffect), the effect will be invoked after milliseconds.

The effect is debounced: Delay restarts whenever dependencies change, and only the last change to dependencies will be used.

function SomeComponent({prop}: {prop: string}) {
    useDebouncedEffect(() => {
        // delays invoking this func until after 500 milliseconds have elapsed 
        // since the last time dependencies changed
    }, [prop], 500)
    return null;
}

FAQs

Package last updated on 21 Apr 2022

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