Skip to content

Instantly share code, notes, and snippets.

@johntran
Last active January 28, 2017 02:04
Show Gist options
  • Save johntran/0e41633fbdc2a96e0925f2d22bac6636 to your computer and use it in GitHub Desktop.
Save johntran/0e41633fbdc2a96e0925f2d22bac6636 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import {isEqual} from 'lodash';
import ProductDetails from 'client/ProductDetails';
// Product Details is a React Component
function optimizeRendering(keys: Array<string>, Wrapped: React<Element>) {
return class OptimizedComponent extends Component {
shouldComponentUpdate(nextProps) {
const changedKeys = keys.filter(key => !isEqual(nextProps[key], this.props[key]));
return changedKeys.length > 0
}
render() {
return (<Wrapped {...this.props}/>)
}
}
}
optimizeRendering(['shopNavOpen', 'containerWidth'], ProductDetails)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment