Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 9b40496

Browse files
committed
Update about memoization.
1 parent 551ab4d commit 9b40496

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

docs/functional-component.html

+4-2
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,9 @@ <h3>Lifecycle Hook Arguments</h3>
370370

371371
<p> </p>
372372
<h2>Memoization Avoids Unncessary Updates</h2>
373-
<p>When you use the <code>update</code> lifecycle hook, it will always fire, even if the data being use has not changed. This can be annoying. You may want <code>update</code> to run only when the component will actually change. Every time you render a functional component with <code>update</code> on it, <code>update</code> will execute. There is one way to get around this--memoization.</p>
373+
<p>When you use the <code>update</code> lifecycle hook, it will always fire when you do a render, even if the data being use has not changed. This does not mean that the DOM was physically updated. That would only happen if the props changed. Every time you render a functional component with <code>update</code> on it, <code>update</code> will execute. There is only one way to get around this--memoization.</p>
374374

375-
<p>Bellow is a Codepen with a list created as a function component. The list items have an <code>onupdate</code> lifecycle hook. When you click on the button that says "Render List with Same Data", it re-renders the list. Notice how in the results you see and oupt for each list item. This is so event though there was no change to the data the list is using. This means there was no physical update of the list items. This is because of how the diffing algorithm works.</p>
375+
<p>Below is a Codepen with a list created as a function component. The list items have an <code>onupdate</code> lifecycle hook. When you click on the button that says "Render List with Same Data", it re-renders the list. Notice how in the results you see and oupt for each list item. This is so event though there was no change to the data the list is using. This means there was no physical update of the list items. This is because of how the diffing algorithm works.</p>
376376

377377
<h3>Normal List without Memoization</h3>
378378
<p>Here's an example of the problem. Notice that as you add items to the list, all the already existing items get output as updated. Also notice that every time you tap the "Render List with Same Data" button, all the items are output as updated, event those you rendered the list with the same data. Infact, because the data was the same, the actual DOM elements would not have been updated.</p>
@@ -397,6 +397,8 @@ <h3>Example of Memoized List</h3>
397397
Robert Biggs (<a href="https://codepen.io/rbiggs">@rbiggs</a>) on <a href="https://codepen.io">CodePen</a>.</p>
398398
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
399399

400+
<p>The above technique solves the problem of the <code>update</code> lifecycle hook firing every time you render a function, even when the data is the same. However, be aware that this increases the memory that the list is using. If your list can be long, and you use this technique on other long list, you may find that your app is consuming a lot of memory, which can lead to other problems. If memoizing list items seems like too much trouble, consider making your list component using the <a target="_blank" href='./class-component.html'>Component class</a>. Class components memoize their previous state so that their update lifecycle hooks only fire when their data changes.</p>
401+
400402

401403
<p> </p>
402404
<h2>Component Class Advantages</h2>

0 commit comments

Comments
 (0)