React.memo

 import { useState, memo } from "react";
 
 function areEqual(prevProps, nextProps) {
   return prevProps.x === nextProps.x;
 }
 
 const Foo = memo(({ x, y }) => {
   console.log("Foo", x, y);
   return <div>Foo</div>;
 });
 
 const Boo = memo(({ x, y }) => {
   console.log("Boo", x, y);
   return <div>Boo</div>;
 }, areEqual);
 
 function App() {
   console.log("App");
   const [count, setCount] = useState(0);
 
   return (
     <div className="App">
         <p>
           <button type="button" onClick={() => setCount((count) => count + 1)}>
             count is: {count}
           </button>
         </p>
         <Foo />
         <Foo x={count} />
         <Foo x={new Date()} />
         <Foo x={999} y={new Date()} />
         <Boo x={count} />
         <Boo x={new Date()} />
         <Boo x={999} y={new Date()} />
     </div>
   );
 }
 
 export default App;

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS