useMemo and useCallback but with a stable cache
Installations
npm install use-memo-one
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
18.4.0
NPM Version
8.12.1
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
alexreardon
Download Statistics
Total Downloads
333,258,132
Last Day
404,811
Last Week
1,747,887
Last Month
7,861,454
Last Year
101,047,630
GitHub Statistics
466 Stars
62 Commits
12 Forks
7 Watching
15 Branches
7 Contributors
Bundle Size
867.00 B
Minified
412.00 B
Minified + Gzipped
Package Meta Information
Latest Version
1.1.3
Package Id
use-memo-one@1.1.3
Unpacked Size
15.41 kB
Size
5.11 kB
File Count
11
NPM Version
8.12.1
Node Version
18.4.0
Total Downloads
Cumulative downloads
Total Downloads
333,258,132
Last day
-8.8%
404,811
Compared to previous day
Last week
-17.7%
1,747,887
Compared to previous week
Last month
4.2%
7,861,454
Compared to previous month
Last year
16.6%
101,047,630
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
1
Dev Dependencies
31
useMemoOne
useMemo
and useCallback
with a stable cache (semantic guarantee)
Background
useMemo
and useCallback
cache the most recent result. However, this cache can be destroyed by React
when it wants to:
You may rely on useMemo as a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance. - React docs
useMemoOne
and useCallbackOne
are concurrent mode
safe alternatives to useMemo
and useCallback
that do provide semantic guarantee. What this means is that you will always get the same reference for a memoized value so long as there is no input change.
Using useMemoOne
and useCallbackOne
will consume more memory than useMemo
and useCallback
in order to provide a stable cache. React
can release the cache of useMemo
and useCallback
, but useMemoOne
will not release the cache until it is garbage collected.
Install
1# npm 2npm install use-memo-one --save 3# yarn 4yarn add use-memo-one
Usage
1import { useMemoOne, useCallbackOne } from 'use-memo-one'; 2 3function App(props) { 4 const { name, age } = props; 5 const value = useMemoOne(() => ({ hello: name }), [name]); 6 const getAge = useCallbackOne(() => age, [age]); 7 8 // ... 9}
Aliased imports
You can use this import
style drop in replacement for useMemo
and useCallback
This style also plays very well with eslint-plugin-react-hooks
.
1import { useMemo, useCallback } from 'use-memo-one';
⚠️ The aliased exports useMemo
and useCallback
will only work if you use only use-memo-one
and will clash if you also use useMemo
or useCallback
from react
1import { useMemo, useCallback } from 'react'; 2// ❌ naming clash 3import { useMemo, useCallback } from 'use-memo-one';
API
See useMemo
and useCallback
Linting
useMemo
and useCallback
have fantastic linting rules with auto fixing in the eslint-plugin-react-hooks
package. In order to take advantage of these with useMemoOne
and useCallbackOne
, structure your import like this:
1import { useMemo, useCallback } from 'use-memo-one'; 2// Or your can alias it yourself 3import { 4 useMemoOne as useMemo, 5 useCallbackOne as useCallback, 6} from 'use-memo-one'; 7 8function App() { 9 const [isActive] = useState(false); 10 11 const onClick = useCallback(() => { 12 console.log('isActive', isActive); 13 14 // the input array will now be correctly checked by eslint-plugin-react-hooks 15 }, [isActive]); 16}
eslint
rules
Here are some eslint
rules you are welcome to use
1module.exports = { 2 rules: { 3 // ...other rules 4 5 'no-restricted-imports': [ 6 'error', 7 { 8 // If you want to force an application to always use useMemoOne 9 paths: [ 10 { 11 name: 'react', 12 importNames: ['useMemo', 'useCallback'], 13 message: 14 '`useMemo` and `useCallback` are subject to cache busting. Please use `useMemoOne`', 15 }, 16 // If you want to force use of the aliased imports from useMemoOne 17 { 18 name: 'use-memo-one', 19 importNames: ['useMemoOne', 'useCallbackOne'], 20 message: 21 'use-memo-one exports `useMemo` and `useCallback` which work nicer with `eslint-plugin-react-hooks`', 22 }, 23 ], 24 }, 25 ], 26 }, 27};
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 5/18 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/validate.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/validate.yml:7: update your workflow using https://app.stepsecurity.io/secureworkflow/alexreardon/use-memo-one/validate.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/validate.yml:8: update your workflow using https://app.stepsecurity.io/secureworkflow/alexreardon/use-memo-one/validate.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 22 are checked with a SAST tool
Reason
55 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-3gx7-xhv7-5mx3
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-w457-6q6x-cgp9
- Warn: Project is vulnerable to: GHSA-62gr-4qp9-h98f
- Warn: Project is vulnerable to: GHSA-f52g-6jhx-586p
- Warn: Project is vulnerable to: GHSA-2cf5-4w76-r9qv
- Warn: Project is vulnerable to: GHSA-3cqr-58rm-57f8
- Warn: Project is vulnerable to: GHSA-g9r4-xpmj-mj65
- Warn: Project is vulnerable to: GHSA-q2c6-c6pm-g3gh
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-h9rv-jmmf-4pgx
- Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
2.7
/10
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More