Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
use-ref-from
Advanced tools
Changelog
[0.1.0] - 2024-04-01
react@>=16.8.0
, by @compulim in PR #47@babel/cli@7.24.1
@babel/core@7.24.3
@babel/plugin-transform-runtime@7.24.3
@babel/preset-env@7.24.3
@babel/preset-react@7.24.1
@babel/preset-typescript@7.24.1
@testing-library/react@14.2.2
@tsconfig/recommended@1.0.4
@tsconfig/strictest@2.0.4
@types/jest@29.5.12
@types/react-dom@18.2.22
@types/react@18.2.70
@typescript-eslint/eslint-plugin@7.4.0
@typescript-eslint/parser@7.4.0
esbuild@0.20.2
eslint-plugin-prettier@5.1.3
eslint-plugin-react@7.34.1
eslint@8.57.0
jest-environment-jsdom@29.7.0
jest@29.7.0
prettier@3.2.5
typescript@5.4.3
Readme
use-ref-from
React.useRef
with an immediate setter and read-only value.
Aids useCallback
and useMemo
for better memoization, reduce wasted render, and properly signal render loop.
By using useRefFrom
, React component developers can make sure their custom hooks are properly memoized.
Following examples shows how useRefFrom
help decoupling mutation for callbacks.
useRefFrom
Return function from useCallback
could change on every render.
const MyComponent = ({ message }: { message: string }) => {
// `handleClick` changes on every update to `message`.
const handleClick = useCallback(() => {
alert(message);
}, [message]);
return <button onClick={handleClick}>Click me</button>;
};
useRefFrom
Decoupling message
from useCallback
by useRef
.
const MyComponent = ({ message }: { message: string }) => {
const messageRef = useRef<string>();
messageRef.current = message;
// `handleClick` stay intact regardless of `message`.
const handleClick = useCallback(() => {
// `messageRef.current` is `string | null`, unsafe casting is required.
alert(messageRef.current as string);
}, [messageRef]);
return <button onClick={handleClick}>Click me</button>;
};
useRefFrom
Decoupling message
from useCallback
by useRefFrom
. It also helps typing and immutability.
const MyComponent = ({ message }: { message: string }) => {
const messageRef = useRefFrom<string>(message);
// `handleClick` stay intact regardless of `message`.
const handleClick = useCallback(() => {
// `messageRef.current` is `readonly string`.
alert(messageRef.current);
// ❌ `messageRef.current` is read-only. The following line will fail at compile-time.
messageRef.current = 'Hello, World!'.
}, [messageRef]);
return <button onClick={handleClick}>Click me</button>;
};
function useRefFrom<T>(value: T): RefObject<T> & { get current(): T };
The RefObject<T>
returned by useRefFrom()
will be read-only and cannot be set. This is by design.
If you see TypeScript compilation error related to useRefFrom.d.ts
, please make sure you are using TypeScript 4.3 or up.
node_modules/use-ref-from/lib/types/useRefFrom.d.ts:3:5 - error TS1131: Property or signature expected.
3 get current(): T;
~~~
Like us? Star us.
Want to make it better? File us an issue.
Don't like something you see? Submit a pull request.
FAQs
React.useRef with an immediate setter and read-only value.
We found that use-ref-from demonstrated a healthy version release cadence and project activity because the last version was released less than 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
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).