rc-pure-component
Version:
a higher-order component to reduce render times quickly
65 lines (42 loc) • 1.56 kB
Markdown
[![npm version][npm-version-image]][npm-url]
[![npm downloads][npm-downloads-image]][npm-url]
[![github issues][github-issues-image]][github-issues-url]
a higher-order component to reduce render times quickly for lazy people like me!
```bash
$ npm install rc-pure-component
$ yarn add rc-pure-component
```
```javascript
const Component = ({ name = 'Hieu' }) => (<div>hello, {name}</div>)
```
```javascript
import pure from 'rc-pure-component'
// normal
const Component = pure(({ name = 'Hieu' }) => (<div>hello, {name}</div>))
// custom
const shallowCompare = (prevProps, nextProps) => !(prevProps === nextProps)
const Component = pure(({ name = 'Hieu' }) => (<div>hello, {name}</div>), shallowCompare)
```
by default I use the calculation of fb, here:
```javascript
import shallowEqual from 'fbjs/lib/shallowEqual'
const shallowCompare = (prevProps, nextProps) => !shallowEqual(prevProps, nextProps)
```
alternatively, you can use the function of the lodash:
```javascript
import isEqual from 'lodash/isEqual'
const shallowCompare = (prevProps, nextProps) => !isEqual(prevProps, nextProps)
```
[]: https://npmjs.org/package/rc-pure-component
[]: https://badge.fury.io/js/rc-pure-component.svg
[]: https://img.shields.io/npm/dm/rc-pure-component.svg
[]: https://img.shields.io/github/issues/lamhieu-vk/rc-pure-component.svg
[]: https://github.com/lamhieu-vk/rc-pure-component/issues