
Security News
Package Maintainers Call for Improvements to GitHub’s New npm Security Plan
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
A set of common utility react hooks.
forwardRef
.Isomorphic useLayout. Safe to use server-side and client-side.
If there is no window
object, falls back to useEffect
.
If there is a window
object, simply is useLayoutEffect
.
Useful for server-side rendered applications where useLayoutEffect
cannot be called server-side.
function WithUseClayoutEffect() {
const [pageRect, setPageRect] = useState<DOMRect | null>(null);
const mainRef = useRef<HTMLDivElement>(null);
// useLayoutEffect is useful for listening to dom elements, but cannot be used
// server-side. useClayoutEffect is safe both server-side and clients-side
useClayoutEffect(() => {
if (!mainRef.current) return;
setPageRect(mainRef.current.getBoundingClientRect());
}, []);
return (
<div ref={mainRef}>
<div>hello world</div>
</div>
)
}
Synchronised DOMRect of the referenced element
Depends on ResizeObserver
.
function WithUseDOMRect() {
const ref = useDomRect((rect) => {
console.log('the new rect is:', rect);
});
return (
<div ref={ref}>
<div>hello world</div>
</div>
)
}
Have effects mounted (has the component rendered and useEffect fired?).
function WithUseHaveEffectsMounted() {
const effectsMounted = useHaveEffectsMounted();
useEffect(() => {
if (!effectsMounted.current) return false;
/** do work here */
}, {});
return (
<main>
<div>hello world</div>
</main>
)
}
Have layout effects mounted (has the component rendered and useLayoutEffect fired?).
function WithUseHaveLayoutsMounted() {
const layoutsMounted = useHaveLayoutsMounted();
useLayoutEffect(() => {
if (!layoutsMounted.current) return false;
/** do work here */
}, {});
return (
<main>
<div>hello world</div>
</main>
)
}
Merges refs together. Useful for forwarding refs.
// merge refs together
const WithUseMergedRefs = React.forwardRef((props, fref) => {
const ref = mergeRefs(fref);
return (
<button {...props} ref={ref}>
click me!
</button>
)
})
Callback that fires on resize
// without selector
function WithUseOnResize() {
const ref = useOnResize<HTMLDivElement>((element: HTMLDivElement) => {
console.log('resized', element.getBoundingClientRect());
});
return <div ref={ref} />;
}
If a selector is supplied, resize events on that selector are listened to instead of the referenced element. The referenced element is given to the callback.
// with selector
function WithUseOnResize() {
const ref = useOnResize<HTMLDivElement>(
// fire when the #root element resizes, instead of the ref'd element
'#root',
(element: HTMLDivElement) => {
console.log('resized', element.getBoundingClientRect());
}
);
return <div ref={ref} />;
}
Maintain a reference to the input value. Useful for functions referencing values that may not be memoized.
interface Props { onSuccess?: () => {} };
function WithUseValueRef({ onSuccess }: Props) {
const _onSuccess = useValueRef(onSuccess);
// the reference to handleSuccess never changes, even when the reference to
// onSuccess does.
const handleSuccess = useCallback(() => {
console.log('successful :)');
_onSuccess.current?.();
}, [_onSuccess]);
return <Form onSuccess={handleSuccess}>;
}
Equivalent to
interface Props { onSuccess?: () => {} };
function Component({ onSuccess }: Props ) {
const _onSuccess = useRef(onSuccess);
_onSuccess.current = onSuccess;
// ...
}
FAQs
A set of useful React hooks
We found that @nkp/hooks demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.