use-callback-ref
Advanced tools
Comparing version 1.2.3 to 1.2.4
import { ReactRef } from './types'; | ||
/** | ||
* Assigns a value for a given ref, no matter of the ref format | ||
* @param {RefObject} ref - a callback function or ref object | ||
* @param value - a new value | ||
* | ||
* @see https://github.com/theKashey/use-callback-ref#assignref | ||
* @example | ||
* const refObject = useRef(); | ||
* const refFn = (ref) => {....} | ||
* | ||
* assignRef(refObject, "refValue"); | ||
* assignRef(refFn, "refValue"); | ||
*/ | ||
export declare function assignRef<T>(ref: ReactRef<T>, value: T | null): ReactRef<T>; |
@@ -0,1 +1,14 @@ | ||
/** | ||
* Assigns a value for a given ref, no matter of the ref format | ||
* @param {RefObject} ref - a callback function or ref object | ||
* @param value - a new value | ||
* | ||
* @see https://github.com/theKashey/use-callback-ref#assignref | ||
* @example | ||
* const refObject = useRef(); | ||
* const refFn = (ref) => {....} | ||
* | ||
* assignRef(refObject, "refValue"); | ||
* assignRef(refFn, "refValue"); | ||
*/ | ||
export function assignRef(ref, value) { | ||
@@ -2,0 +15,0 @@ if (typeof ref === 'function') { |
import { RefObject } from 'react'; | ||
/** | ||
* creates a Ref object with on change callback | ||
* @param callback | ||
* @returns {RefObject} | ||
* | ||
* @see {@link useCallbackRef} | ||
* @see https://reactjs.org/docs/refs-and-the-dom.html#creating-refs | ||
*/ | ||
export declare function createCallbackRef<T>(callback: (newValue: T | null, lastValue: T | null) => any): RefObject<T>; |
@@ -0,1 +1,9 @@ | ||
/** | ||
* creates a Ref object with on change callback | ||
* @param callback | ||
* @returns {RefObject} | ||
* | ||
* @see {@link useCallbackRef} | ||
* @see https://reactjs.org/docs/refs-and-the-dom.html#creating-refs | ||
*/ | ||
export function createCallbackRef(callback) { | ||
@@ -2,0 +10,0 @@ var current = null; |
@@ -1,3 +0,16 @@ | ||
import * as React from 'react'; | ||
import { MutableRefObject } from 'react'; | ||
import { ReactRef } from './types'; | ||
export declare function mergeRefs<T>(refs: ReactRef<T>[]): React.MutableRefObject<T | null>; | ||
/** | ||
* Merges two or more refs together providing a single interface to set their value | ||
* @param {RefObject|Ref} refs | ||
* @returns {MutableRefObject} - a new ref, which translates all changes to {refs} | ||
* | ||
* @see {@link useMergeRefs} to be used in ReactComponents | ||
* @example | ||
* const Component = React.forwardRef((props, ref) => { | ||
* const ownRef = useRef(); | ||
* const domRef = mergeRefs([ref, ownRef]); // 👈 merge together | ||
* return <div ref={domRef}>...</div> | ||
* } | ||
*/ | ||
export declare function mergeRefs<T>(refs: ReactRef<T>[]): MutableRefObject<T | null>; |
import { createCallbackRef } from './createRef'; | ||
import { assignRef } from './assignRef'; | ||
/** | ||
* Merges two or more refs together providing a single interface to set their value | ||
* @param {RefObject|Ref} refs | ||
* @returns {MutableRefObject} - a new ref, which translates all changes to {refs} | ||
* | ||
* @see {@link useMergeRefs} to be used in ReactComponents | ||
* @example | ||
* const Component = React.forwardRef((props, ref) => { | ||
* const ownRef = useRef(); | ||
* const domRef = mergeRefs([ref, ownRef]); // 👈 merge together | ||
* return <div ref={domRef}>...</div> | ||
* } | ||
*/ | ||
export function mergeRefs(refs) { | ||
@@ -4,0 +17,0 @@ return createCallbackRef(function (newValue) { |
import { ReactRef, RefCallback } from './types'; | ||
/** | ||
* Unmemoized version of {@link useRefToCallback} | ||
* @see {@link useRefToCallback} | ||
* @param ref | ||
*/ | ||
export declare function refToCallback<T>(ref: ReactRef<T>): RefCallback<T>; | ||
/** | ||
* Transforms a given `ref` into `callback`. | ||
* | ||
* To transform `callback` into ref use {@link useCallbackRef|useCallbackRef(undefined, callback)} | ||
* | ||
* @param {ReactRef} ref | ||
* @returns {Function} | ||
* | ||
* @see https://github.com/theKashey/use-callback-ref#reftocallback | ||
* | ||
* @example | ||
* const ref = useRef(0); | ||
* const setRef = useRefToCallback(ref); | ||
* 👉 setRef(10); | ||
* ✅ ref.current === 10 | ||
*/ | ||
export declare function useRefToCallback<T>(ref: ReactRef<T>): RefCallback<T>; |
@@ -0,1 +1,6 @@ | ||
/** | ||
* Unmemoized version of {@link useRefToCallback} | ||
* @see {@link useRefToCallback} | ||
* @param ref | ||
*/ | ||
export function refToCallback(ref) { | ||
@@ -24,4 +29,20 @@ return function (newValue) { | ||
}; | ||
/** | ||
* Transforms a given `ref` into `callback`. | ||
* | ||
* To transform `callback` into ref use {@link useCallbackRef|useCallbackRef(undefined, callback)} | ||
* | ||
* @param {ReactRef} ref | ||
* @returns {Function} | ||
* | ||
* @see https://github.com/theKashey/use-callback-ref#reftocallback | ||
* | ||
* @example | ||
* const ref = useRef(0); | ||
* const setRef = useRefToCallback(ref); | ||
* 👉 setRef(10); | ||
* ✅ ref.current === 10 | ||
*/ | ||
export function useRefToCallback(ref) { | ||
return weakMemoize(ref); | ||
} |
import * as React from 'react'; | ||
import { ReactRef } from './types'; | ||
/** | ||
* Merges two or more refs together providing a single interface to set their value | ||
* @param {RefObject|Ref} refs | ||
* @returns {MutableRefObject} - a new ref, which translates all changes to {refs} | ||
* | ||
* @see {@link mergeRefs} a version without buit-in memoization | ||
* @see https://github.com/theKashey/use-callback-ref#usemergerefs | ||
* @example | ||
* const Component = React.forwardRef((props, ref) => { | ||
* const ownRef = useRef(); | ||
* const domRef = useMergeRefs([ref, ownRef]); // 👈 merge together | ||
* return <div ref={domRef}>...</div> | ||
* } | ||
*/ | ||
export declare function useMergeRefs<T>(refs: ReactRef<T>[], defaultValue?: T): React.MutableRefObject<T | null>; |
import { useCallbackRef } from './useRef'; | ||
import { assignRef } from './assignRef'; | ||
/** | ||
* Merges two or more refs together providing a single interface to set their value | ||
* @param {RefObject|Ref} refs | ||
* @returns {MutableRefObject} - a new ref, which translates all changes to {refs} | ||
* | ||
* @see {@link mergeRefs} a version without buit-in memoization | ||
* @see https://github.com/theKashey/use-callback-ref#usemergerefs | ||
* @example | ||
* const Component = React.forwardRef((props, ref) => { | ||
* const ownRef = useRef(); | ||
* const domRef = useMergeRefs([ref, ownRef]); // 👈 merge together | ||
* return <div ref={domRef}>...</div> | ||
* } | ||
*/ | ||
export function useMergeRefs(refs, defaultValue) { | ||
@@ -4,0 +18,0 @@ return useCallbackRef(defaultValue, function (newValue) { |
import { MutableRefObject } from 'react'; | ||
/** | ||
* creates a MutableRef with ref change callback | ||
* @param initialValue - initial ref value | ||
* @param {Function} callback - a callback to run when value changes | ||
* | ||
* @example | ||
* const ref = useCallbackRef(0, (newValue, oldValue) => console.log(oldValue, '->', newValue); | ||
* ref.current = 1; | ||
* // prints 0 -> 1 | ||
* | ||
* @see https://reactjs.org/docs/hooks-reference.html#useref | ||
* @see https://github.com/theKashey/use-callback-ref#usecallbackref---to-replace-reactuseref | ||
* @returns {MutableRefObject} | ||
*/ | ||
export declare function useCallbackRef<T>(initialValue: T | null, callback: (newValue: T | null, lastValue: T | null) => void): MutableRefObject<T | null>; |
import { useState } from 'react'; | ||
/** | ||
* creates a MutableRef with ref change callback | ||
* @param initialValue - initial ref value | ||
* @param {Function} callback - a callback to run when value changes | ||
* | ||
* @example | ||
* const ref = useCallbackRef(0, (newValue, oldValue) => console.log(oldValue, '->', newValue); | ||
* ref.current = 1; | ||
* // prints 0 -> 1 | ||
* | ||
* @see https://reactjs.org/docs/hooks-reference.html#useref | ||
* @see https://github.com/theKashey/use-callback-ref#usecallbackref---to-replace-reactuseref | ||
* @returns {MutableRefObject} | ||
*/ | ||
export function useCallbackRef(initialValue, callback) { | ||
@@ -3,0 +17,0 @@ var ref = useState(function () { return ({ |
import { ReactRef, RefObject } from './types'; | ||
/** | ||
* Create a _lense_ on Ref, making it possible to transform ref value | ||
* @param {ReactRef} ref | ||
* @param {Function} transformer. 👉 Ref would be __NOT updated__ on `transformer` update. | ||
* @returns {RefObject} | ||
* | ||
* @see https://github.com/theKashey/use-callback-ref#usetransformref-to-replace-reactuseimperativehandle | ||
* @example | ||
* | ||
* const ResizableWithRef = forwardRef((props, ref) => | ||
* <Resizable {...props} ref={useTransformRef(ref, i => i ? i.resizable : null)}/> | ||
* ); | ||
*/ | ||
export declare function useTransformRef<T, K>(ref: ReactRef<K>, transformer: (original: T) => K): RefObject<T>; |
import { useCallbackRef } from './useRef'; | ||
import { assignRef } from './assignRef'; | ||
/** | ||
* Create a _lense_ on Ref, making it possible to transform ref value | ||
* @param {ReactRef} ref | ||
* @param {Function} transformer. 👉 Ref would be __NOT updated__ on `transformer` update. | ||
* @returns {RefObject} | ||
* | ||
* @see https://github.com/theKashey/use-callback-ref#usetransformref-to-replace-reactuseimperativehandle | ||
* @example | ||
* | ||
* const ResizableWithRef = forwardRef((props, ref) => | ||
* <Resizable {...props} ref={useTransformRef(ref, i => i ? i.resizable : null)}/> | ||
* ); | ||
*/ | ||
export function useTransformRef(ref, transformer) { | ||
@@ -4,0 +17,0 @@ return useCallbackRef(undefined, function (value) { |
import { ReactRef } from './types'; | ||
/** | ||
* Assigns a value for a given ref, no matter of the ref format | ||
* @param {RefObject} ref - a callback function or ref object | ||
* @param value - a new value | ||
* | ||
* @see https://github.com/theKashey/use-callback-ref#assignref | ||
* @example | ||
* const refObject = useRef(); | ||
* const refFn = (ref) => {....} | ||
* | ||
* assignRef(refObject, "refValue"); | ||
* assignRef(refFn, "refValue"); | ||
*/ | ||
export declare function assignRef<T>(ref: ReactRef<T>, value: T | null): ReactRef<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Assigns a value for a given ref, no matter of the ref format | ||
* @param {RefObject} ref - a callback function or ref object | ||
* @param value - a new value | ||
* | ||
* @see https://github.com/theKashey/use-callback-ref#assignref | ||
* @example | ||
* const refObject = useRef(); | ||
* const refFn = (ref) => {....} | ||
* | ||
* assignRef(refObject, "refValue"); | ||
* assignRef(refFn, "refValue"); | ||
*/ | ||
function assignRef(ref, value) { | ||
@@ -4,0 +17,0 @@ if (typeof ref === 'function') { |
import { RefObject } from 'react'; | ||
/** | ||
* creates a Ref object with on change callback | ||
* @param callback | ||
* @returns {RefObject} | ||
* | ||
* @see {@link useCallbackRef} | ||
* @see https://reactjs.org/docs/refs-and-the-dom.html#creating-refs | ||
*/ | ||
export declare function createCallbackRef<T>(callback: (newValue: T | null, lastValue: T | null) => any): RefObject<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* creates a Ref object with on change callback | ||
* @param callback | ||
* @returns {RefObject} | ||
* | ||
* @see {@link useCallbackRef} | ||
* @see https://reactjs.org/docs/refs-and-the-dom.html#creating-refs | ||
*/ | ||
function createCallbackRef(callback) { | ||
@@ -4,0 +12,0 @@ var current = null; |
@@ -1,3 +0,16 @@ | ||
import * as React from 'react'; | ||
import { MutableRefObject } from 'react'; | ||
import { ReactRef } from './types'; | ||
export declare function mergeRefs<T>(refs: ReactRef<T>[]): React.MutableRefObject<T | null>; | ||
/** | ||
* Merges two or more refs together providing a single interface to set their value | ||
* @param {RefObject|Ref} refs | ||
* @returns {MutableRefObject} - a new ref, which translates all changes to {refs} | ||
* | ||
* @see {@link useMergeRefs} to be used in ReactComponents | ||
* @example | ||
* const Component = React.forwardRef((props, ref) => { | ||
* const ownRef = useRef(); | ||
* const domRef = mergeRefs([ref, ownRef]); // 👈 merge together | ||
* return <div ref={domRef}>...</div> | ||
* } | ||
*/ | ||
export declare function mergeRefs<T>(refs: ReactRef<T>[]): MutableRefObject<T | null>; |
@@ -5,2 +5,15 @@ "use strict"; | ||
var assignRef_1 = require("./assignRef"); | ||
/** | ||
* Merges two or more refs together providing a single interface to set their value | ||
* @param {RefObject|Ref} refs | ||
* @returns {MutableRefObject} - a new ref, which translates all changes to {refs} | ||
* | ||
* @see {@link useMergeRefs} to be used in ReactComponents | ||
* @example | ||
* const Component = React.forwardRef((props, ref) => { | ||
* const ownRef = useRef(); | ||
* const domRef = mergeRefs([ref, ownRef]); // 👈 merge together | ||
* return <div ref={domRef}>...</div> | ||
* } | ||
*/ | ||
function mergeRefs(refs) { | ||
@@ -7,0 +20,0 @@ return createRef_1.createCallbackRef(function (newValue) { |
import { ReactRef, RefCallback } from './types'; | ||
/** | ||
* Unmemoized version of {@link useRefToCallback} | ||
* @see {@link useRefToCallback} | ||
* @param ref | ||
*/ | ||
export declare function refToCallback<T>(ref: ReactRef<T>): RefCallback<T>; | ||
/** | ||
* Transforms a given `ref` into `callback`. | ||
* | ||
* To transform `callback` into ref use {@link useCallbackRef|useCallbackRef(undefined, callback)} | ||
* | ||
* @param {ReactRef} ref | ||
* @returns {Function} | ||
* | ||
* @see https://github.com/theKashey/use-callback-ref#reftocallback | ||
* | ||
* @example | ||
* const ref = useRef(0); | ||
* const setRef = useRefToCallback(ref); | ||
* 👉 setRef(10); | ||
* ✅ ref.current === 10 | ||
*/ | ||
export declare function useRefToCallback<T>(ref: ReactRef<T>): RefCallback<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Unmemoized version of {@link useRefToCallback} | ||
* @see {@link useRefToCallback} | ||
* @param ref | ||
*/ | ||
function refToCallback(ref) { | ||
@@ -27,2 +32,18 @@ return function (newValue) { | ||
}; | ||
/** | ||
* Transforms a given `ref` into `callback`. | ||
* | ||
* To transform `callback` into ref use {@link useCallbackRef|useCallbackRef(undefined, callback)} | ||
* | ||
* @param {ReactRef} ref | ||
* @returns {Function} | ||
* | ||
* @see https://github.com/theKashey/use-callback-ref#reftocallback | ||
* | ||
* @example | ||
* const ref = useRef(0); | ||
* const setRef = useRefToCallback(ref); | ||
* 👉 setRef(10); | ||
* ✅ ref.current === 10 | ||
*/ | ||
function useRefToCallback(ref) { | ||
@@ -29,0 +50,0 @@ return weakMemoize(ref); |
import * as React from 'react'; | ||
import { ReactRef } from './types'; | ||
/** | ||
* Merges two or more refs together providing a single interface to set their value | ||
* @param {RefObject|Ref} refs | ||
* @returns {MutableRefObject} - a new ref, which translates all changes to {refs} | ||
* | ||
* @see {@link mergeRefs} a version without buit-in memoization | ||
* @see https://github.com/theKashey/use-callback-ref#usemergerefs | ||
* @example | ||
* const Component = React.forwardRef((props, ref) => { | ||
* const ownRef = useRef(); | ||
* const domRef = useMergeRefs([ref, ownRef]); // 👈 merge together | ||
* return <div ref={domRef}>...</div> | ||
* } | ||
*/ | ||
export declare function useMergeRefs<T>(refs: ReactRef<T>[], defaultValue?: T): React.MutableRefObject<T | null>; |
@@ -5,2 +5,16 @@ "use strict"; | ||
var assignRef_1 = require("./assignRef"); | ||
/** | ||
* Merges two or more refs together providing a single interface to set their value | ||
* @param {RefObject|Ref} refs | ||
* @returns {MutableRefObject} - a new ref, which translates all changes to {refs} | ||
* | ||
* @see {@link mergeRefs} a version without buit-in memoization | ||
* @see https://github.com/theKashey/use-callback-ref#usemergerefs | ||
* @example | ||
* const Component = React.forwardRef((props, ref) => { | ||
* const ownRef = useRef(); | ||
* const domRef = useMergeRefs([ref, ownRef]); // 👈 merge together | ||
* return <div ref={domRef}>...</div> | ||
* } | ||
*/ | ||
function useMergeRefs(refs, defaultValue) { | ||
@@ -7,0 +21,0 @@ return useRef_1.useCallbackRef(defaultValue, function (newValue) { |
import { MutableRefObject } from 'react'; | ||
/** | ||
* creates a MutableRef with ref change callback | ||
* @param initialValue - initial ref value | ||
* @param {Function} callback - a callback to run when value changes | ||
* | ||
* @example | ||
* const ref = useCallbackRef(0, (newValue, oldValue) => console.log(oldValue, '->', newValue); | ||
* ref.current = 1; | ||
* // prints 0 -> 1 | ||
* | ||
* @see https://reactjs.org/docs/hooks-reference.html#useref | ||
* @see https://github.com/theKashey/use-callback-ref#usecallbackref---to-replace-reactuseref | ||
* @returns {MutableRefObject} | ||
*/ | ||
export declare function useCallbackRef<T>(initialValue: T | null, callback: (newValue: T | null, lastValue: T | null) => void): MutableRefObject<T | null>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var react_1 = require("react"); | ||
/** | ||
* creates a MutableRef with ref change callback | ||
* @param initialValue - initial ref value | ||
* @param {Function} callback - a callback to run when value changes | ||
* | ||
* @example | ||
* const ref = useCallbackRef(0, (newValue, oldValue) => console.log(oldValue, '->', newValue); | ||
* ref.current = 1; | ||
* // prints 0 -> 1 | ||
* | ||
* @see https://reactjs.org/docs/hooks-reference.html#useref | ||
* @see https://github.com/theKashey/use-callback-ref#usecallbackref---to-replace-reactuseref | ||
* @returns {MutableRefObject} | ||
*/ | ||
function useCallbackRef(initialValue, callback) { | ||
@@ -5,0 +19,0 @@ var ref = react_1.useState(function () { return ({ |
import { ReactRef, RefObject } from './types'; | ||
/** | ||
* Create a _lense_ on Ref, making it possible to transform ref value | ||
* @param {ReactRef} ref | ||
* @param {Function} transformer. 👉 Ref would be __NOT updated__ on `transformer` update. | ||
* @returns {RefObject} | ||
* | ||
* @see https://github.com/theKashey/use-callback-ref#usetransformref-to-replace-reactuseimperativehandle | ||
* @example | ||
* | ||
* const ResizableWithRef = forwardRef((props, ref) => | ||
* <Resizable {...props} ref={useTransformRef(ref, i => i ? i.resizable : null)}/> | ||
* ); | ||
*/ | ||
export declare function useTransformRef<T, K>(ref: ReactRef<K>, transformer: (original: T) => K): RefObject<T>; |
@@ -5,2 +5,15 @@ "use strict"; | ||
var assignRef_1 = require("./assignRef"); | ||
/** | ||
* Create a _lense_ on Ref, making it possible to transform ref value | ||
* @param {ReactRef} ref | ||
* @param {Function} transformer. 👉 Ref would be __NOT updated__ on `transformer` update. | ||
* @returns {RefObject} | ||
* | ||
* @see https://github.com/theKashey/use-callback-ref#usetransformref-to-replace-reactuseimperativehandle | ||
* @example | ||
* | ||
* const ResizableWithRef = forwardRef((props, ref) => | ||
* <Resizable {...props} ref={useTransformRef(ref, i => i ? i.resizable : null)}/> | ||
* ); | ||
*/ | ||
function useTransformRef(ref, transformer) { | ||
@@ -7,0 +20,0 @@ return useRef_1.useCallbackRef(undefined, function (value) { |
{ | ||
"name": "use-callback-ref", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "The same useRef, but with callback", | ||
@@ -5,0 +5,0 @@ "main": "dist/es5/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32069
710