Skip to content

Instantly share code, notes, and snippets.

@atmd83
Created February 27, 2018 13:03
Show Gist options
  • Save atmd83/a05edd0eb4924481ecde6269efcebf05 to your computer and use it in GitHub Desktop.
Save atmd83/a05edd0eb4924481ecde6269efcebf05 to your computer and use it in GitHub Desktop.
medium
import React from 'react';
import PubSub from 'pubsub-js';
export default class Thing extends React.Component {
constructor(props) {
super(props);
this.state = {
message: 'The value is'
}
}
componentDidMount() {
PubSub.publish('thing.did.mount');
}
componentWillReceiveProps(nextProps, nextState) {
PubSub.publish('thing.will.receive.props');
return true
}
shouldComponentUpdate(nextProps, nextState) {
if (this.props.value !== nextProps.value || this.state.message !== nextState.message) {
PubSub.publish('thing.should.update');
return true;
}
return false;
}
componentWillUpdate(nextProps, nextState) {
PubSub.publish('thing.will.update');
return true;
}
componentWillUnmount() {
PubSub.publish('thing.will.unmount');
}
render() {
return (<h1>{`${this.state.message} ${this.props.value}`}</h1>);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment