JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 15495618
  • Score
    100M100P100Q219844F
  • License MIT

The same useRef, but with callback

Package Exports

  • use-callback-ref

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (use-callback-ref) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

🤙 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

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
}

License

MIT