Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var pureRender = (Component) => {
Object.assign(Component.prototype, {
shouldComponentUpdate (nextProps, nextState) {
return !shallowEqual(this.props, nextProps) ||
!shallowEqual(this.state, nextState);
}
});
};
module.exports = pureRender;
///////////////////////////////////////////////////
var pureRender = require('./pureRender');
class Foo extends React.Component {
render () {
return <div>this ain’t so bad</div>
}
}
pureRender(Foo); // don't return so you know its mutative :(
module.exports = Foo;
@benmosher

This comment has been minimized.

Show comment
Hide comment
@benmosher

benmosher Mar 30, 2015

Interesting class-y mixin pattern! but why check shallowEqual(this.state, nextState)?

Interesting class-y mixin pattern! but why check shallowEqual(this.state, nextState)?

@adaniliuk

This comment has been minimized.

Show comment
Hide comment
@adaniliuk

adaniliuk Mar 30, 2015

@benmosher , how do you know whether props or state is updated? Component should rerender in either case, right?

@benmosher , how do you know whether props or state is updated? Component should rerender in either case, right?

@RickWong

This comment has been minimized.

Show comment
Hide comment
@RickWong

RickWong Mar 30, 2015

Unfortunately this.state of the child becomes meaningless in the above implementation.

If you really want to do this right, you'll need to pass this.setState() as a prop to the child component, and make it use it like this.props.setState() instead of the child's own this.setState().

Unfortunately this.state of the child becomes meaningless in the above implementation.

If you really want to do this right, you'll need to pass this.setState() as a prop to the child component, and make it use it like this.props.setState() instead of the child's own this.setState().

@ryanflorence

This comment has been minimized.

Show comment
Hide comment
@ryanflorence

ryanflorence Mar 30, 2015

j/k this won't work

Owner

ryanflorence commented Mar 30, 2015

j/k this won't work

@ryanflorence

This comment has been minimized.

Show comment
Hide comment
@ryanflorence

ryanflorence Mar 30, 2015

updated, I can't think of a very declarative way to do this except to mutate the prototype or inherit :\

Owner

ryanflorence commented Mar 30, 2015

updated, I can't think of a very declarative way to do this except to mutate the prototype or inherit :\

@RickWong

This comment has been minimized.

Show comment
Hide comment
@RickWong

RickWong Mar 30, 2015

@ryanflorence You can still Object.assign({}, ...) and return a mutated clone.

@ryanflorence You can still Object.assign({}, ...) and return a mutated clone.

@aputinski

This comment has been minimized.

Show comment
Hide comment
@aputinski

aputinski Mar 30, 2015

How do you feel about this approach?

class Foo extends React.Component {
  constructor(props) {
    super(props);
    this.shouldComponentUpdate = React.addons.PureRenderMixin.shouldComponentUpdate.bind(this);
  }
  render () {
    return <div>Helllo</div>
  }
}

How do you feel about this approach?

class Foo extends React.Component {
  constructor(props) {
    super(props);
    this.shouldComponentUpdate = React.addons.PureRenderMixin.shouldComponentUpdate.bind(this);
  }
  render () {
    return <div>Helllo</div>
  }
}
@brigand

This comment has been minimized.

Show comment
Hide comment
@brigand

brigand Mar 30, 2015

@aputinski you get a gold star for simplest possible solution :-)

In any cases that are more complex than a single lifecycle hook, this doesn't work as well.

@RickWong methods on es6 classes aren't enumerable, but you could do Object.assign(Object.create(Foo), {should...})

brigand commented Mar 30, 2015

@aputinski you get a gold star for simplest possible solution :-)

In any cases that are more complex than a single lifecycle hook, this doesn't work as well.

@RickWong methods on es6 classes aren't enumerable, but you could do Object.assign(Object.create(Foo), {should...})

@timbur

This comment has been minimized.

Show comment
Hide comment
@timbur

timbur May 3, 2015

Does the latest gist work without any issues? Hard to tell by the comments. :)

timbur commented May 3, 2015

Does the latest gist work without any issues? Hard to tell by the comments. :)

@keeth

This comment has been minimized.

Show comment
Hide comment

keeth commented Oct 9, 2015

@aputinski 👍

@captDaylight

This comment has been minimized.

Show comment
Hide comment
@carlesba

This comment has been minimized.

Show comment
Hide comment
@carlesba

carlesba Jan 7, 2016

Won't be easier just extending Component?

class PureRenderComponent extends React.Component {
  shouldComponentUpdate () {
    return React.addons.PureRenderMixin.shouldComponentUpdate.apply(this, arguments)
  }
}

class Foo extends PureRenderComponent {
  render () {
    return (<div>Pure</div>)
  }
}

carlesba commented Jan 7, 2016

Won't be easier just extending Component?

class PureRenderComponent extends React.Component {
  shouldComponentUpdate () {
    return React.addons.PureRenderMixin.shouldComponentUpdate.apply(this, arguments)
  }
}

class Foo extends PureRenderComponent {
  render () {
    return (<div>Pure</div>)
  }
}
@alexFaunt

This comment has been minimized.

Show comment
Hide comment
@alexFaunt

alexFaunt Mar 16, 2016

Sorry to pile on, but this is one of the top hits when searching for PureRenderMixin in ES6, my two cents:

const pure = function (target) {
  target.prototype.shouldComponentUpdate = React.addons.PureRenderMixin.shouldComponentUpdate;
  return target;
}

@pure
class Test extends React.Component { }

E.g. https://jsbin.com/zayoda/4/edit?js,console,output

Sorry to pile on, but this is one of the top hits when searching for PureRenderMixin in ES6, my two cents:

const pure = function (target) {
  target.prototype.shouldComponentUpdate = React.addons.PureRenderMixin.shouldComponentUpdate;
  return target;
}

@pure
class Test extends React.Component { }

E.g. https://jsbin.com/zayoda/4/edit?js,console,output

@jameswnl

This comment has been minimized.

Show comment
Hide comment
@jameswnl

jameswnl Apr 26, 2016

Hi, is the code provided by @aputinski still works? I'm using react 15.0.1 and it doesn't work for me.

jameswnl commented Apr 26, 2016

Hi, is the code provided by @aputinski still works? I'm using react 15.0.1 and it doesn't work for me.

@yoshi415

This comment has been minimized.

Show comment
Hide comment
@yoshi415

yoshi415 Apr 28, 2016

@jameswnl
From official docs:

import PureRenderMixin from 'react-addons-pure-render-mixin';
class FooComponent extends React.Component {
  constructor(props) {
    super(props);
    this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
  }

  render() {
    return <div className={this.props.className}>foo</div>;
  }
}

You need to install the addon as well.

@jameswnl
From official docs:

import PureRenderMixin from 'react-addons-pure-render-mixin';
class FooComponent extends React.Component {
  constructor(props) {
    super(props);
    this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
  }

  render() {
    return <div className={this.props.className}>foo</div>;
  }
}

You need to install the addon as well.

@dlong500

This comment has been minimized.

Show comment
Hide comment
@dlong500

dlong500 May 14, 2016

@brigand
Can you elaborate on your statement "In any cases that are more complex than a single lifecycle hook, this doesn't work as well".

What exactly are the drawbacks to this same approach that is also now listed in the official React docs?

@brigand
Can you elaborate on your statement "In any cases that are more complex than a single lifecycle hook, this doesn't work as well".

What exactly are the drawbacks to this same approach that is also now listed in the official React docs?

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