Skip to content

Commit 31f559f

Browse files
committed
feat: add primitives module use-callback-ref subpkg
1 parent 7bcca06 commit 31f559f

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"name": "soybean-react-ui/use-callback-ref",
3+
"version": "1.1.1",
4+
"license": "MIT",
5+
"homepage": "https://radix-ui.com/primitives",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/radix-ui/primitives.git"
9+
},
10+
"bugs": {
11+
"url": "https://github.com/radix-ui/primitives/issues"
12+
},
13+
"publishConfig": {
14+
"main": "./dist/index.js",
15+
"module": "./dist/index.mjs",
16+
"types": "./dist/index.d.ts",
17+
"exports": {
18+
".": {
19+
"import": {
20+
"types": "./dist/index.d.mts",
21+
"default": "./dist/index.mjs"
22+
},
23+
"require": {
24+
"types": "./dist/index.d.ts",
25+
"default": "./dist/index.js"
26+
}
27+
}
28+
}
29+
},
30+
"sideEffects": false,
31+
"main": "./src/index.ts",
32+
"module": "./src/index.ts",
33+
"files": ["dist", "README.md"],
34+
"scripts": {
35+
"build": "radix-build",
36+
"clean": "rm -rf dist",
37+
"lint": "eslint --max-warnings 0 src",
38+
"typecheck": "tsc --noEmit"
39+
},
40+
"peerDependencies": {
41+
"@types/react": "*",
42+
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
43+
},
44+
"peerDependenciesMeta": {
45+
"@types/react": {
46+
"optional": true
47+
}
48+
},
49+
"devDependencies": {
50+
"@types/react": "^19.0.7",
51+
"@types/react-dom": "^19.0.3",
52+
"eslint": "^9.18.0",
53+
"react": "^19.1.0",
54+
"react-dom": "^19.1.0",
55+
"typescript": "^5.7.3"
56+
},
57+
"source": "./src/index.ts"
58+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { useCallbackRef } from './use-callback-ref';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as React from 'react';
2+
3+
/**
4+
* A custom hook that converts a callback to a ref to avoid triggering re-renders when passed as a
5+
* prop or avoid re-executing effects when passed as a dependency
6+
*/
7+
function useCallbackRef<T extends (...args: any[]) => any>(callback: T | undefined): T {
8+
const callbackRef = React.useRef(callback);
9+
10+
React.useEffect(() => {
11+
callbackRef.current = callback;
12+
});
13+
14+
// https://github.com/facebook/react/issues/19240
15+
return React.useMemo(() => ((...args) => callbackRef.current?.(...args)) as T, []);
16+
}
17+
18+
export { useCallbackRef };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
},
6+
"include": ["src"],
7+
"exclude": ["node_modules", "dist"]
8+
}

0 commit comments

Comments
 (0)