All Projects → theKashey → Use Callback Ref

theKashey / Use Callback Ref

🤙The same useRef, but it will callback

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Use Callback Ref

async-chainable
An extension to Async adding better handling of mixed Series / Parallel tasks via object chaining
Stars: ✭ 25 (-84.08%)
Mutual labels:  hooks, callback
React Infinite Scroll Hook
A simple hook to create infinite scroll list components
Stars: ✭ 151 (-3.82%)
Mutual labels:  hooks
Pure Store
A tiny immutable store with type safety.
Stars: ✭ 133 (-15.29%)
Mutual labels:  hooks
Tabs
React tabs with hooks
Stars: ✭ 147 (-6.37%)
Mutual labels:  hooks
Poloniex Api Node
Poloniex API client for REST and WebSocket API
Stars: ✭ 138 (-12.1%)
Mutual labels:  callback
React Universal Hooks
🎉 React Universal Hooks : just use****** everywhere (Functional or Class Component). Support React DevTools!
Stars: ✭ 148 (-5.73%)
Mutual labels:  hooks
React Use Scrollspy
Flexible React Hook to automatically update navigation based on scroll position
Stars: ✭ 133 (-15.29%)
Mutual labels:  hooks
React Intersection Observer
React implementation of the Intersection Observer API to tell you when an element enters or leaves the viewport.
Stars: ✭ 2,689 (+1612.74%)
Mutual labels:  hooks
React Use Clipboard
React hook that provides copy to clipboard functionality.
Stars: ✭ 149 (-5.1%)
Mutual labels:  hooks
React Storage Hooks
React hooks for persistent state
Stars: ✭ 146 (-7.01%)
Mutual labels:  hooks
React Permissible
👮‍♂️Making the permission management for React components easier.
Stars: ✭ 145 (-7.64%)
Mutual labels:  callback
Jquery Confirm
A multipurpose plugin for alert, confirm & dialog, with extended features.
Stars: ✭ 1,776 (+1031.21%)
Mutual labels:  callback
Distormx
The ultimate hooking library
Stars: ✭ 146 (-7.01%)
Mutual labels:  hooks
Use Less
React hooks that help you do what you already did, with more indirection
Stars: ✭ 135 (-14.01%)
Mutual labels:  hooks
React Model
The next generation state management library for React
Stars: ✭ 153 (-2.55%)
Mutual labels:  hooks
React Atom
A simple way manage state in React, inspired by Clojure(Script) and reagent.cljs
Stars: ✭ 133 (-15.29%)
Mutual labels:  hooks
Precommit
pre-commit hooks for R projects
Stars: ✭ 141 (-10.19%)
Mutual labels:  hooks
Use State With Callback
Custom hook to include a callback function for useState.
Stars: ✭ 147 (-6.37%)
Mutual labels:  callback
React Native Web Hooks
Hooks for React Native web and Expo
Stars: ✭ 157 (+0%)
Mutual labels:  hooks
Use Url State
Lift a React component's state into the url
Stars: ✭ 154 (-1.91%)
Mutual labels:  hooks

🤙 use-callback-ref 📞


The same `useRef` but it will callback: 📞 Hello! Your ref was 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

Read more about use-callback pattern and use cases:

This library exposes helpers to handle any case related to ref lifecycle

  • useCallbackRef - react on hook change
  • mergeRefs - merge multiple refs together. For, actually, fork
  • transformRef - transform one ref to anther
  • refToCallback - convert RefObject to an old callback-style ref
  • assignRef - assign value to the ref, regardless of it's form

All functions are tree shakable, but even together it's less then 300b.

API

💡 Some commands are hooks based, and returns the same refs/functions every render. But some are not, to be used in classes or non-react code.

useRef API

🤔 Use case: every time you have to react to ref change

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

createCallbackRef - to replace React.createRef

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

useCallbackRef - to replace React.useRef

  • useCallbackRef(initialValue, callback) - 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}/>

assignRef

🤔 Use case: every time you need to assign ref manually, and you dont know the shape of the ref

assignRef(ref, value) - assigns values to the ref. ref could be RefObject or RefCallback.

🚫 ref.current = value // what if it's a callback-ref?
🚫 ref(value) // but what if it's a object ref?

import {assignRef} from "use-callback-ref";
✅ assignRef(ref, value); 

useTransformRef (to replace React.useImperativeHandle)

🤔 Use case: ref could be different. transformRef(ref, tranformer):Ref - return a new ref which would propagate all changes to the provided ref with applied transform

// before
const ResizableWithRef = forwardRef((props, ref) =>
  <Resizable {...props} ref={i => i && ref(i.resizable)}/>
);

// after

const ResizableWithRef = forwardRef((props, ref) =>
  <Resizable {...props} ref={transformRef(ref, i => i ? i.resizable : null)}/>
);

refToCallback

refToCallback(ref: RefObject): RefCallback - for compatibility between the old and the new code. For the compatibility between RefCallback and RefObject use useCallbackRef(undefined, callback)

useMergeRefs

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

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.

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

const MergedComponent = React.forwardRef((props, ref) => {
  const localRef = React.useRef();
  // ...
  // both localRef and ref would be populated with the `ref` to a `div`
  return <div ref={useMergeRefs([localRef, ref])} />
})

💡 - useMergeRefs will always give you the same return, and you don't have to worry about [localRef, ref] unique every render.

mergeRefs

mergeRefs(refs: arrayOfRefs, [defaultValue]):ReactMutableRef - merges a few refs together is a non-hook based version. Will produce the new ref every run, causing the old one to unmount, and be populated with the null value.

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

mergeRefs are "safe" to use as a part of other hooks-based commands, but don't forget - it returns a new object every call.

Similar packages:

  • apply-ref - applyRefs is simular to mergeRef, applyRef is similar to assignRef
  • useForkRef - useForkRef is simular to useMergeRefs, but accepts only two arguments.
  • react-merge-refs - merge-refs is simular to useMergeRefs, but not a hook and does not provide "stable" reference.

Is it a rocket science? No, RefObject is no more than {current: ref}, and use-callback-ref is no more than getter and setter on that field.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].