Gathering detailed insights and metrics for react-lazy-cache
Gathering detailed insights and metrics for react-lazy-cache
Gathering detailed insights and metrics for react-lazy-cache
Gathering detailed insights and metrics for react-lazy-cache
A utility to lazily calculate and cache values in a react component based on props
npm install react-lazy-cache
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
44 Stars
21 Commits
6 Forks
3 Watching
1 Branches
3 Contributors
Updated on 08 Feb 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-10.8%
3,472
Compared to previous day
Last week
1.6%
19,623
Compared to previous week
Last month
21.3%
83,897
Compared to previous month
Last year
-12.5%
1,167,081
Compared to previous year
1
react-lazy-cache
is a utility to lazily calculate and cache values in a React component based on props.
npm install --save react-lazy-cache
Ideally, in a React component, you would calculate values that depend on your props inputs every time the component
is rendered. However, in practice, sometimes these values, either for computational or memory reasons, are better off
cached. When you cache them, however, you need to be constantly watching your props to know if you need to
invalidate your cache and recalculate those values. That is what react-lazy-cache
does for you.
react-lazy-cache
could not be simpler to use. You simply need to give it a map of calculations, and let it know
when your component will receive new props.
1import React, {Component, PropTypes} from 'react'; 2import lazyCache from 'react-lazy-cache'; 3 4export default class Arithmetic extends Component { 5 static propTypes = { 6 a: PropTypes.number.isRequired, 7 b: PropTypes.number.isRequired 8 } 9 10 componentWillMount() { 11 // create cache 12 this.cache = lazyCache(this, { 13 sum: { 14 params: ['a', 'b'], 15 fn: (a, b) => a + b 16 }, 17 difference: { 18 params: ['a', 'b'], 19 fn: (a, b) => a - b 20 }, 21 product: { 22 params: ['a', 'b'], 23 fn: (a, b) => a * b 24 }, 25 quotient: { 26 params: ['a', 'b'], 27 fn: (a, b) => a / b 28 }, 29 sumSquared: { 30 params: ['sum'], 31 fn: (sum) => sum * sum 32 } 33 }); 34 } 35 36 componentWillReceiveProps(nextProps) { 37 this.cache.componentWillReceiveProps(nextProps); 38 } 39 40 render() { 41 const {sum, difference, product, quotient, sumSquared} = this.cache; 42 return (<div> 43 <div>Sum: {sum}</div> 44 <div>Difference: {difference}</div> 45 <div>Product: {product}</div> 46 <div>Quotient: {quotient}</div> 47 <div>Sum Squared: {sumSquared}</div> 48 </div>); 49 } 50}
Two things to notice about the above example:
The values do not get calculated until the properties on the cache
object get referenced in render().
That's why it's "lazy". They will not be calculated again unless one of the props that the calculation depends on
changes.
When you specify your functions to calculate each value, you must specify the params
, which refer either to props
given to your React component, or to other calculated values (see: sumSquared
).
Be careful to not cause an infinite dependency loop!
As this library utilizes Getters, which are not shimmable in IE8 and older, an alternate noGetters
module is exposed.
This version allows you to cache values, but are not able to inject other values such as sumSquared
. Usage:
1import LazyCache from 'react-lazy-cache/noGetters'; 2 3const cache = new LazyCache(...) // same signature as normal version 4 5const sum = cache.get('sum');
The difference is that it's a class and not a plain function (so you have to new
it), and properties are accessed
through the get
-function, instead of as a property.
That's all you need to know! Go forth and intelligently cache your calculated values!
Feedback welcome.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 3/19 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
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