https://github.com/jonathanong/fn-key-cache
https://github.com/jonathanong/fn-key-cache
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jonathanong/fn-key-cache
- Owner: jonathanong
- License: mit
- Created: 2017-02-12T08:37:08.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-03-15T16:32:14.000Z (about 5 years ago)
- Last Synced: 2025-01-21T09:53:24.690Z (5 months ago)
- Language: JavaScript
- Size: 14.6 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fn-key-cache
[![NPM version][npm-image]][npm-url]
[![Build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![Dependency Status][david-image]][david-url]
[](https://greenkeeper.io/)
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]Little utility to cache functions based on keys.
Useful for creating handlers in React:```js
import React, { Component } from 'react'import KeyCache from 'fn-key-cache'
export default class Thing extends Component {
state = {
tab: 1
}onClickButton = KeyCache(tab => e => {
e.preventDefault()this.setState({
tab
})
})render () {
const { tab } = this.statereturn (
1
2
3
)
}
}
```Without this utility, new functions would be created on every `render()`.
This is better than doing something like `onClick={e => this.setState({ tab: 1 })}`,
but not better than just creating a new component.[npm-image]: https://img.shields.io/npm/v/fn-key-cache.svg?style=flat-square
[npm-url]: https://npmjs.org/package/fn-key-cache
[travis-image]: https://img.shields.io/travis/jonathanong/fn-key-cache/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/jonathanong/fn-key-cache
[codecov-image]: https://img.shields.io/codecov/c/github/jonathanong/fn-key-cache/master.svg?style=flat-square
[codecov-url]: https://codecov.io/github/jonathanong/fn-key-cache
[david-image]: http://img.shields.io/david/jonathanong/fn-key-cache.svg?style=flat-square
[david-url]: https://david-dm.org/jonathanong/fn-key-cache
[license-image]: http://img.shields.io/npm/l/fn-key-cache.svg?style=flat-square
[license-url]: LICENSE
[downloads-image]: http://img.shields.io/npm/dm/fn-key-cache.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/fn-key-cache