Socket
Socket
Sign inDemoInstall

use-callback-ref

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-callback-ref - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

dist/es2015/mergeRef.d.ts

2

dist/es2015/createRef.d.ts
import { RefObject } from "react";
export declare function createCallbackRef<T>(callback: (newValue: T, lastValue: T | null) => any): RefObject<T>;
export declare function createCallbackRef<T>(callback: (newValue: T | null, lastValue: T | null) => any): RefObject<T>;
export { useCallbackRef } from './useRef';
export { createCallbackRef } from './createRef';
export { mergeRefs } from './mergeRef';
export { useCallbackRef } from './useRef';
export { createCallbackRef } from './createRef';
export { mergeRefs } from './mergeRef';
import { MutableRefObject } from 'react';
export declare function useCallbackRef<T>(initialValue: T, callback: (newValue: T, lastValue: T) => void): MutableRefObject<T>;
export declare function useCallbackRef<T>(initialValue: T | null, callback: (newValue: T | null, lastValue: T | null) => void): MutableRefObject<T | null>;
import { RefObject } from "react";
export declare function createCallbackRef<T>(callback: (newValue: T, lastValue: T | null) => any): RefObject<T>;
export declare function createCallbackRef<T>(callback: (newValue: T | null, lastValue: T | null) => any): RefObject<T>;
export { useCallbackRef } from './useRef';
export { createCallbackRef } from './createRef';
export { mergeRefs } from './mergeRef';

@@ -7,1 +7,3 @@ "use strict";

exports.createCallbackRef = createRef_1.createCallbackRef;
var mergeRef_1 = require("./mergeRef");
exports.mergeRefs = mergeRef_1.mergeRefs;
import { MutableRefObject } from 'react';
export declare function useCallbackRef<T>(initialValue: T, callback: (newValue: T, lastValue: T) => void): MutableRefObject<T>;
export declare function useCallbackRef<T>(initialValue: T | null, callback: (newValue: T | null, lastValue: T | null) => void): MutableRefObject<T | null>;
{
"name": "use-callback-ref",
"version": "1.0.1",
"version": "1.1.0",
"description": "The same useRef, but with callback",

@@ -42,3 +42,13 @@ "main": "dist/es5/index.js",

"createRef"
],
"size-limit": [
{
"path": "dist/es2015/index.js",
"limit": "0.5 KB"
},
{
"path": "dist/es2015/useRef.js",
"limit": "0.5 KB"
}
]
}

@@ -25,2 +25,3 @@ <div align="center">

# API
## useRef API
API is 99% compatible with React `createRef` and `useRef`, and just adds another argument - `callback`,

@@ -52,3 +53,32 @@ which would be called on __ref update__.

💡 You can use `useCallbackRef` to convert RefObject into RefCallback, creating bridges between the old and the new code
```js
// 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
```js
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
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc