Skip to content

Instantly share code, notes, and snippets.

@googya
Created April 8, 2018 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save googya/ec7d807878750fdade367de1841cc80d to your computer and use it in GitHub Desktop.
Save googya/ec7d807878750fdade367de1841cc80d to your computer and use it in GitHub Desktop.
import React from "react";
import { render } from "react-dom";
const log = name => {
console.log("----------------------");
console.log(name);
};
class Example extends React.Component {
constructor(props) {
super(props);
this.state = {};
log("Constructor");
}
static getDerivedStateFromProps(nextProps, prevState) {
log("getDerivedStateFromProps");
return null;
}
componentDidMount() {
log("componentDidMount");
}
componentDidUpdate(prevProps, prevState, snapshot) {
log("ComponentDidUpdate");
}
componentWillUnmount() {
log("componentWillUnmount");
}
shouldComponentUpdate(nextProps, nextState) {
log("shouldComponentUpdate");
return true;
}
getSnapshotBeforeUpdate(prevProps, prevState) {
log("getSnapshotBeforeUpdate");
return null;
}
render() {
log("render");
return (
<div>
<b>Example Component</b>
</div>
);
}
}
class Root extends React.Component {
constructor(props) {
super(props);
this.state = { vals: 0, visible: false };
}
toggle = () => {
log(`*** Triggering ${this.state.visible ? "Unmount" : "Mount"} ***`);
this.setState(state => ({ visible: !state.visible }));
};
update = () => {
log("*** Triggering Update ***");
this.setState(state => ({ vals: state.vals + 1 }));
};
render() {
return (
<div>
<h2>The Component Lifecycle in React 16.3</h2>
{this.state.visible && <Example />}
{this.state.visible && <button onClick={this.update}>Update</button>}
<button onClick={this.toggle}>
{this.state.visible ? "Remove" : "Add"} Component
</button>
</div>
);
}
}
render(<Root />, document.getElementById("root"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment