Skip to content

Commit

Permalink
chap 8.4 modify sample code to use 'useMemo'
Browse files Browse the repository at this point in the history
  • Loading branch information
ye-geeee committed Sep 6, 2021
1 parent 5dc94f4 commit b64150c
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions basic/src/Average.js
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useMemo } from 'react';

const getAverage = numbers => {
console.log('Calculating average...');
Expand All @@ -21,6 +21,8 @@ const Average = () => {
setNumber('');
};

const avg = useMemo(() => getAverage(list), [list]);

return (
<div>
<input value={number} onChange={onChange} />
Expand All @@ -31,7 +33,7 @@ const Average = () => {
))}
</ul>
<div>
<b>Average:</b>{getAverage(list)}
<b>Average:</b>{avg}
</div>
</div>
);
Expand Down

0 comments on commit b64150c

Please sign in to comment.