https://github.com/dashed/react-prop-once
Higher-order component to cache props once on initial mount, and propagate them on every other render
https://github.com/dashed/react-prop-once
Last synced: 5 days ago
JSON representation
Higher-order component to cache props once on initial mount, and propagate them on every other render
- Host: GitHub
- URL: https://github.com/dashed/react-prop-once
- Owner: dashed
- Created: 2015-06-12T01:04:43.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-09-21T03:44:15.000Z (over 10 years ago)
- Last Synced: 2024-12-30T08:26:02.498Z (about 1 year ago)
- Language: JavaScript
- Size: 141 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# react-prop-once
> Higher-order component to cache props once on initial mount, and propagate them on every other render
Useful for resolving values (e.g. `props`) once after initial mount and re-using them as props on every render, rather than resolving them on every `render()` call. These resolved props are merged with every `nextProps`.
Internally, props are cached in `getInitialState()`. This practice is [considered an anti-Pattern](https://facebook.github.io/react/tips/props-in-getInitialState-as-anti-pattern.html), but through this higher-order component, this is the intended behaviour.
## Usage
```
$ npm install --save react-prop-once
```
```js
const once = require('react-prop-once');
function assignPropsOnMount(props) {
// called once on every mount
// ...
return {
answer: 42
};
}
function cleanOnUnmount(cached) {
// called once on every unmount
// optional clean up step
// cached is object returned by assignPropsOnMount(...)
}
const WrappedComponent = once(Component, assignPropsOnMount, cleanOnUnmount);
```
## License
MIT