I have a question on using useCallback on built-in/ non-custom components in React. Below I provided a simple example.
I wrapped "increment1" in useCallback so it would not be recreated and passed as a new function into MyOwnButton component. Thus, with React.memo, MyOwnButton component would only get rerendered when 'count1' is updated.
Here come my question. Should I wrap "increment2" in useCallback too? So it would not be passed as a new function into the tag (or simplified as "button2") and to prevent "button2" from unnecessary rerendering too?
import React, { memo, useCallback, useState } from 'react';
export default function App() {
const [count1, setCount1] = useState(0);
const [count2, setCount2] = useState(0);
const increment1 = useCallback(() => {
setCount1((prev) => prev + 1);
}, []);
const increment2 = () => setCount2((prev) => prev + 2);
return (
<>
<MyOwnButton count={count1} onClick={increment1} />
<button onClick={increment2}>button2:{count2}</button>
</>
);
}
const MyOwnButton = memo(({ count, onClick }: any) => {
console.log('MyOwnButton');
return <button onClick={onClick}>button1:{count}</button>;
});