Skip to content

Instantly share code, notes, and snippets.

@yogurt1
Last active February 19, 2018 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogurt1/d69e2999b97a2f1ba9b09bc649c9c593 to your computer and use it in GitHub Desktop.
Save yogurt1/d69e2999b97a2f1ba9b09bc649c9c593 to your computer and use it in GitHub Desktop.
React <Memoization />
import React from 'react';
import PropTypes from 'prop-types';
class Memoization extends React.Component {
lastInput = null;
result = null;
render() {
const { equals, input, compute, children } = this.props;
if (!equals(input, this.lastInput)) {
this.result = compute(input);
this.lastInput = input;
}
return children(this.result);
}
}
// Usage
<Memoize
equals={shallowEqual}
input={...}
compute={input => ...}
children={result => ...}
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment