A React mixin that provides shallow comparison optimization for shouldComponentUpdate to prevent unnecessary re-renders
npx @tessl/cli install tessl/npm-react-addons-pure-render-mixin@15.6.0React Pure Render Mixin provides a performance optimization mixin for React components that implements shouldComponentUpdate with shallow comparison. It prevents unnecessary re-renders when props and state haven't changed, improving component performance through optimized render lifecycle management.
npm install react-addons-pure-render-mixinvar PureRenderMixin = require('react-addons-pure-render-mixin');ES6 import:
import PureRenderMixin from 'react-addons-pure-render-mixin';Script tag (after React):
<!-- development version -->
<script src="https://unpkg.com/react-addons-pure-render-mixin/react-addons-pure-render-mixin.js"></script>
<!-- Access via React.addons.PureRenderMixin -->const createReactClass = require('create-react-class');
const MyComponent = createReactClass({
mixins: [PureRenderMixin],
render: function() {
return <div className={this.props.className}>
{this.props.children}
</div>;
}
});
// The mixin automatically provides shouldComponentUpdate optimization
// Component will only re-render when props or state actually changeProvides automatic shouldComponentUpdate implementation with shallow comparison optimization.
/**
* Mixin object containing shouldComponentUpdate method
* Exported as the default export of the package
* @type {Object}
*/
const PureRenderMixin = {
/**
* shouldComponentUpdate lifecycle method with shallow comparison
* Compares current props/state with next props/state using shallow equality
* @param {Object} nextProps - The next props that will be passed to component
* @param {Object} nextState - The next state that will be set on component
* @returns {boolean} - true if component should update (props or state changed), false to skip re-render
*/
shouldComponentUpdate: function(nextProps, nextState) {
return (
!shallowEqual(this.props, nextProps) ||
!shallowEqual(this.state, nextState)
);
}
};Usage Example:
const React = require('react');
const createReactClass = require('create-react-class');
const PureRenderMixin = require('react-addons-pure-render-mixin');
const OptimizedComponent = createReactClass({
mixins: [PureRenderMixin],
getInitialState: function() {
return {
count: 0,
message: 'Hello'
};
},
increment: function() {
this.setState({ count: this.state.count + 1 });
},
render: function() {
return (
<div>
<p>{this.state.message}: {this.state.count}</p>
<button onClick={this.increment}>Increment</button>
</div>
);
}
});
// Component automatically skips re-render when:
// - Same props object reference is passed
// - setState is called with identical state values
// - Parent re-renders but passes same propsThe mixin performs shallow (first-level) comparison only:
=== equality)// These changes WILL trigger re-render:
this.setState({ count: 1 }); // primitive change
this.setState({ items: newArray }); // array reference change
// These changes will NOT trigger re-render:
this.state.items.push(newItem); // mutating existing array
this.state.user.name = 'New Name'; // mutating nested object
// To trigger re-render with nested changes, create new references:
this.setState({
items: [...this.state.items, newItem],
user: { ...this.state.user, name: 'New Name' }
});React.PureComponent (available since React 15.3.0) for ES6 classesReact.memo() for functional components// Modern equivalent using React.PureComponent:
class ModernComponent extends React.PureComponent {
render() {
return <div>{this.props.content}</div>;
}
}
// Or with React.memo for functional components:
const FunctionalComponent = React.memo(function(props) {
return <div>{props.content}</div>;
});shallowEqual utility function for comparison logicThe mixin does not throw exceptions under normal usage. It safely handles:
null or undefined props/state valuesIf comparison fails, it defaults to allowing the update (returns true).