Skip to content

Instantly share code, notes, and snippets.

@yogurt1 yogurt1/Memoization.jsx
Last active Feb 19, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.