Socket
Socket
Sign inDemoInstall

use-callback-ref

Package Overview
Dependencies
6
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    use-callback-ref

The same useRef, but with callback


Version published
Maintainers
1
Install size
10.1 kB
Created

Package description

What is use-callback-ref?

The use-callback-ref npm package provides React hooks for creating and managing callback refs in a more convenient and powerful way. It allows for the creation of refs that can execute a callback function whenever the ref changes, enabling more reactive and dynamic interactions with DOM elements or component instances in React applications.

What are use-callback-ref's main functionalities?

Creating a callback ref

This feature allows the creation of a callback ref that executes a callback function whenever the ref's current element changes. In this example, the callback updates the state with the height of the element.

import { useCallbackRef } from 'use-callback-ref';

const MyComponent = () => {
  const [height, setHeight] = useState(0);
  const ref = useCallbackRef(null, (element) => {
    if (element !== null) {
      setHeight(element.getBoundingClientRect().height);
    }
  });

  return <div ref={ref}>Content</div>;
};

Using a callback ref with useEffect

This demonstrates how to use a callback ref in conjunction with useEffect to perform actions whenever the ref's current element changes. The callback ref updates a state variable, which triggers the useEffect hook.

import { useCallbackRef } from 'use-callback-ref';
import { useEffect } from 'react';

const MyComponent = () => {
  const [node, setNode] = useState(null);
  const ref = useCallbackRef(null, setNode);

  useEffect(() => {
    if (node) {
      // Perform actions with the node
    }
  }, [node]);

  return <div ref={ref}>Content</div>;
};

Other packages similar to use-callback-ref

Changelog

Source

1.1.0 (2019-10-08)

Features

  • add mergeRef (32278f9)

Readme

Source

🤙 use-callback-ref 📞


Hey! Your ref just got changed!
Travis bundle size
--- > Keep in mind that useRef doesn't notify you when its content changes. Mutating the .current property doesn't cause a re-render. If you want to run some code when React attaches or detaches a ref to a DOM node, you may want to use ~~a callback ref instead~~ .... __useCallbackRef__ instead.

Hooks API Reference

API

useRef API

API is 99% compatible with React createRef and useRef, and just adds another argument - callback, which would be called on ref update.

  • createCallbackRef(callback) - (aka React.createRef) would call provided callback when ref is changed.

  • useRef(initialValue, callback) - (aka React.useRef)would call provided callback when ref is changed.

  • callback in both cases is callback(newValue, oldValue). Callback would not be called if newValue and oldValue is the same.

import {useRef, createRef, useState} from 'react';
import {useCallbackRef, createCallbackRef} from 'use-callback-ref';

const Component = () => {
  const [,forceUpdate] = useState();
  // I dont need callback when ref changes
  const ref = useRef(null); 
  
  // but sometimes - it could be what you need
  const anotherRef = useCallbackRef(null, () => forceUpdate());
  
  useEffect( () => {
    // now it's just possible
  }, [anotherRef.current]) // react to dom node change
}

💡 You can use useCallbackRef to convert RefObject into RefCallback, creating bridges between the old and the new code

// some old component
const onRefUpdate = (newValue) => {...}
const refObject = useCallbackRef(null, onRefUpdate);
// ...
<SomeNewComponent ref={refObject}/>

Additional API

mergeRefs

mergeRefs(refs: arrayOfRefs, [defaultValue]):ReactMutableRef - merges a few refs together

import React from 'react'
import {mergeRefs} from 'use-callback-ref'

const MergedComponent = React.forwardRef(function Example(props, ref) {
  const localRef = React.useRef()
  return <div ref={mergeRefs([localRef, ref])} />
})

based on https://github.com/smooth-code/react-merge-refs, just exposes RefObject, instead of callback

When developing low level UI components, it is common to have to use a local ref but also support an external one using React.forwardRef. Natively, React does not offer a way to set two refs inside the ref property. This is the goal of this small utility.

License

MIT

Keywords

FAQs

Last updated on 08 Oct 2019

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc