Skip to content

Instantly share code, notes, and snippets.

@djsubstance
Created August 17, 2023 12:00
Show Gist options
  • Save djsubstance/5bddbcd3ce2c21c5f63561340104746a to your computer and use it in GitHub Desktop.
Save djsubstance/5bddbcd3ce2c21c5f63561340104746a to your computer and use it in GitHub Desktop.
React components lifecycle diagram
<img src="data:image/svg+xml;base64,PHN2ZyBpZD0iTGF5ZXJfMSIgZGF0YS1uYW1lPSJMYXllciAxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyMyAyMC40NjM0OCI+PHRpdGxlPmxvZ288L3RpdGxlPjxwYXRoIGQ9Ik0xOC45MTA3LDYuNjMyNTdoMHEtLjM2NzIxLS4xMjYtLjc0MDQyLS4yMzMzLjA2MTg3LS4yNTE0MS4xMTQ0MS0uNTA1Yy41NjA0NS0yLjcyMDY0LjE5NC00LjkxMjM3LTEuMDU3MzktNS42MzM4Ni0xLjE5OTgtLjY5Mi0zLjE2MjEuMDI5NTItNS4xNDM5NCwxLjc1NDE0cS0uMjkyOTMuMjU1NS0uNTcyNjcuNTI1NTQtLjE4NzI3LS4xNzk1MS0uMzgxMS0uMzUyQzkuMDUyNTcuMzQzOSw2Ljk3MDY2LS40MzMxNiw1LjcyMDU4LjI5MDQ2LDQuNTIxOTEuOTg0MzYsNC4xNjY4NiwzLjA0NDg5LDQuNjcxNDQsNS42MjMyMnEuMDc1My4zODMuMTcuNzYxNzljLS4yOTQ1OC4wODM2Ny0uNTc5MDguMTcyODQtLjg1MTI3LjI2NzcxQzEuNTU1MTQsNy41MDE2NSwwLDguODMyMjUsMCwxMC4yMTIzMWMwLDEuNDI1NDYsMS42NjkzNSwyLjg1NTIsNC4yMDU3NSwzLjcyMnEuMzA4NS4xMDQ5NC42MjE5My4xOTQ0Mi0uMTAxNzkuNDA4LS4xODA2OC44MjExNGMtLjQ4MTA2LDIuNTMzNTQtLjEwNTM1LDQuNTQ1MjEsMS4wOTAxNyw1LjIzNDg0L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xQzIyLjAxNzUzLDExLjEwMTQ1LDIwLjYxNTM4LDEyLjI1NTc0LDE4LjMzODg1LDEzLjAxMDFaIiBmaWxsPSIjNjFkYWZiIi8+PHBhdGggZD0iTTExLjUsOC4xNTg1YTIuMDUzODYsMi4wNTM4NiwwLDEsMS0yLjA1MzgxLDIuMDUzODFBMi4wNTM4MSwyLjA1MzgxLDAsMCwxLDExLjUsOC4xNTg1IiBmaWxsPSIjNjFkYWZiIi8+PC9zdmc+" alt="" class="logo">
<ul class="descriptions">
<li class="description" id="getInitialState">
<h2 class="heading heading--2">getInitialState</h2>
<p>Invoked once before the component is mounted. The return value will be used as the initial value of <code>this.state</code></p>
</li>
<li class="description" id="componentWillMount">
<h2 class="heading heading--2">componentWillMount</h2>
<p>Invoked once, both on the client and server, immediately before the initial rendering occurs. If you call <code>setState</code> within this method, <code>render()</code> will see the updated state and will be executed only once despite the state change.</p>
</li>
<li class="description" id="render">
<h2 class="heading heading--2">render</h2>
<p>The render() method is required.</p>
<p>When called, it should examine this.props and this.state and return a single child element. This child element can be either a virtual representation of a native DOM component (such as <code>&lt;div /&gt;</code> or <code>React.DOM.div()</code>) or another composite component that you've defined yourself.</p>
<p>You can also return null or false to indicate that you don't want anything rendered. Behind the scenes, React renders a <code>&lt;noscript&gt;</code> tag to work with our current diffing algorithm. When returning null or false, <code>ReactDOM.findDOMNode(this)</code> will return <code>null</code>.</p>
<p>The <code>render()</code> function should be pure, meaning that it does not modify component state, it returns the same result each time it's invoked, and it does not read from or write to the DOM or otherwise interact with the browser (e.g., by using <code>setTimeout</code>). If you need to interact with the browser, perform your work in <code>componentDidMount()</code> or the other lifecycle methods instead. Keeping <code>render()</code> pure makes server rendering more practical and makes components easier to think about.</p>
</li>
<li class="description" id="componentDidMount">
<h2 class="heading heading--2">componentDidMount</h2>
<p>Invoked once, only on the client (not on the server), immediately after the initial rendering occurs. At this point in the lifecycle, you can access any refs to your children (e.g., to access the underlying DOM representation). The <code>componentDidMount()</code> method of child components is invoked before that of parent components.</p>
<p>If you want to integrate with other JavaScript frameworks, set timers using setTimeout or setInterval, or send AJAX requests, perform those operations in this method.</p>
</li>
<li class="description" id="componentWillReceiveProps">
<h2 class="heading heading--2">componentWillReceiveProps</h2>
<p>Invoked when a component is receiving new props. This method is not called for the initial render.</p>
<p>Use this as an opportunity to react to a prop transition before <code>render()</code> is called by updating the state using <code>this.setState()</code>. The old props can be accessed via this.props. Calling <code>this.setState()</code> within this function will not trigger an additional render.</p>
</li>
<li class="description" id="shouldComponentUpdate">
<h2 class="heading heading--2">shouldComponentUpdate</h2>
<p>Invoked before rendering when new props or state are being received. This method is not called for the initial render or when <code>forceUpdate</code> is used.</p>
<p>Use this as an opportunity to return <code>false</code> when you're certain that the transition to the new props and state will not require a component update.</p>
</li>
<li class="description" id="componentWillUpdate">
<h2 class="heading heading--2">componentWillUpdate</h2>
<p>Invoked immediately before rendering when new props or state are being received. This method is not called for the initial render.</p>
<p>Use this as an opportunity to perform preparation before an update occurs.</p>
</li>
<li class="description" id="componentDidUpdate">
<h2 class="heading heading--2">componentDidUpdate</h2>
<p>Invoked immediately after the component's updates are flushed to the DOM. This method is not called for the initial render.</p>
<p>Use this as an opportunity to operate on the DOM when the component has been updated.</p>
</li>
<li class="description" id="componentWillUnmount">
<h2 class="heading heading--2">componentWillUnmount</h2>
<p>Invoked immediately before a component is unmounted from the DOM.</p>
<p>Perform any necessary cleanup in this method, such as invalidating timers or cleaning up any DOM elements that were created in <code>componentDidMount</code>.</p>
</li>
<li class="description description--placeholder">
<h2 class="heading heading--2">React components lifecycle diagram</h2>
<p>Click on a method to see a description.</p>
<p>Taken from the <a href="https://facebook.github.io/react/docs/component-specs.html#lifecycle-methods">React documentation page</a>, compiled by <a href="https://eduardoboucas.com">Eduardo Bouças</a>.</p>
</li>
<li class="description__close"><a href="#">×</a></li>
</ul>
<div class="lanes">
<ul class="lane">
<li class="lane__title">Mounting</li>
<li class="lane__item"><a class="step" href="#getInitialState">getInitialState</a></li>
<li class="lane__item"><a class="step" href="#componentWillMount">componentWillMount</a></li>
<li class="lane__item"><a class="step" href="#render">render</a></li>
<li class="lane__item"><a class="step" href="#componentDidMount">componentDidMount</a></li>
</ul>
<ul class="lane">
<li class="lane__title">Updating</li>
<li class="lane__item"><a class="step" href="#componentWillReceiveProps">componentWillReceiveProps</a></li>
<li class="lane__item"><a class="step" href="#shouldComponentUpdate">shouldComponentUpdate</a></li>
<li class="lane__item"><a class="step" href="#componentWillUpdate">componentWillUpdate</a></li>
<li class="lane__item"><a class="step" href="#render">render</a></li>
<li class="lane__item"><a class="step" href="#componentDidUpdate">componentDidUpdate</a></li>
</ul>
<ul class="lane">
<li class="lane__title">Unmounting</li>
<li class="lane__item"><a class="step" href="#componentWillUnmount">componentWillUnmount</a></li>
</ul>
</div>

React components lifecycle diagram

A diagram showing the sequence of methods invoked in the lifecycle of a React component

A Pen by substance on CodePen.

License.

$colour-accent: #61dafb;
$colour-background: #222;
$colour-background-shade-2: #2d2d2d;
p {
margin-top: 20px;
line-height: 1.6;
}
a {
color: $colour-accent;
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
code {
background-color: $colour-background;
font-family: Monaco, monospace;
font-size: 0.9em;
padding: 5px;
}
.logo {
display: block;
margin: 30px auto 0 auto;
width: 100px;
height: 100px;
}
.heading {
margin-bottom: 0.5em;
}
.heading--2 {
font-size: 30px;
}
.lanes {
display: inline-block;
}
.lane {
padding: 50px 0;
text-align: left;
}
.lane + .lane {
border-top: 1px solid white;
}
.lane__title {
display: inline-block;
color: white;
margin-right: 10px;
font-size: 12px;
width: 80px;
text-align: right;
}
.lane__item {
display: inline-block;
margin-right: -4px;
& + & {
&:before {
content: '➞';
color: white;
font-size: 1em;
vertical-align: middle;
margin: 0 0.5em;
}
}
}
.step {
display: inline-block;
color: $colour-accent;
text-decoration: none;
font-size: 15px;
border: 3px solid $colour-accent;
border-radius: 10px;
padding: 15px;
transition: all 0.1s ease-out;
&:hover {
background-color: $colour-accent;
color: black;
text-decoration: none;
}
}
.descriptions {
position: relative;
background-color: $colour-background-shade-2;
color: white;
padding: 20px;
border-radius: 10px;
min-height: 100px;
max-width: 730px;
margin: 40px auto;
}
.description {
display: none;
&:target,
&--placeholder {
display: block;
}
&:target ~ &--placeholder {
display: none;
}
&:target ~ &__close {
display: block;
}
&:not(.description--placeholder) {
text-align: left;
}
}
.description__close {
position: absolute;
top: 15px;
right: 15px;
font-size: 25px;
transition: opacity 0.2s ease-out;
display: none;
}
body {
background-color: $colour-background;
font-family: 'Montserrat', sans-serif;
text-align: center;
}
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment