/* this is for es6 onward */
/* React.memo is the new PureComponent */
import React, { memo } from 'react'
const MyComponent = () => {
return <>something</>
}
export default memo(MyComponent)
function MyComponent(props) {
/* render using props */
}
function areEqual(prevProps, nextProps) {
/*
return true if passing nextProps to render would return
the same result as passing prevProps to render,
otherwise return false
*/
}
export default React.memo(MyComponent, areEqual);
export function MyReactComponent({ myNumber }) {
return <p>{myNumber * 5}</p>
}
export default React.memo(MyReactComponent, function areEqual(
prevProps,
nextProps
) {
if (prevProps.myNumber !== nextProps.myNumber) {
return false
}
return true
})
//index.js:
import { useState } from "react";
import ReactDOM from "react-dom/client";
import Todos from "./Todos";
const App = () => {
const [count, setCount] = useState(0);
const [todos, setTodos] = useState(["todo 1", "todo 2"]);
const increment = () => {
setCount((c) => c + 1);
};
return (
<>
<Todos todos={todos} />
<hr />
<div>
Count: {count}
<button onClick={increment}>+</button>
</div>
</>
);
};
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);