Skip to content

Instantly share code, notes, and snippets.

@developit
Created April 25, 2017 11:43
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save developit/6e518a2ea4f2da03b0a8e4a2f81bc362 to your computer and use it in GitHub Desktop.
Save developit/6e518a2ea4f2da03b0a8e4a2f81bc362 to your computer and use it in GitHub Desktop.
PureComponent for preact
import { Component } from 'preact';
export default class PureComponent extends Component {
shouldComponentUpdate(props, state) {
return !(shallowEqual(props, this.props) && shallowEqual(state, this.state));
}
}
function shallowEqual(a, b) {
for (let key in a) if (a[key]!==b[key]) return false;
for (let key in b) if (!(key in a)) return false;
return true;
}
@nojvek
Copy link

nojvek commented Mar 12, 2020

@developit why don't you make this part of preact so we can just do import {PureComponent} from 'preact'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment