Skip to content

Commit aeae980

Browse files
committed
コールバック関数を useCallback フックでメモ化
1 parent 188b2d6 commit aeae980

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/App.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import { memo, useMemo, useState } from 'react';
1+
import { memo, useCallback, useMemo, useState } from 'react';
22

33
const App = () => {
44
console.log('renders <App />');
55

66
const [item, setItem] = useState('');
77
const [cartItems, setCartItems] = useState([]);
88

9-
const handleChange = (event) => {
9+
const handleChange = useCallback((event) => {
1010
setItem(event.target.value);
11-
};
11+
}, []);
1212

13-
const handleAdd = () => {
13+
const handleAdd = useCallback(() => {
1414
if (cartItems.includes(item)) {
1515
return;
1616
}
1717

1818
setCartItems((items) => [...items, item]);
1919
setItem('');
20-
};
20+
}, [cartItems, item]);
2121

22-
const handleClear = () => {
22+
const handleClear = useCallback(() => {
2323
setCartItems([]);
24-
};
24+
}, []);
2525

2626
return (
2727
<div style={{ margin: '10px' }}>

0 commit comments

Comments
 (0)