What is React? JSX Introduction and Your First Component
What problem React actually solves, how JSX syntax works, and how to build your first component.
Every re-render runs a component's function body from scratch — meaning any expensive calculations and new function objects inside it get recreated too. useMemo and useCallback give you a way to "cache" both of those things.
function ProductList({ products, searchTerm }) {
const filteredProducts = useMemo(() => {
console.log('Filtering...'); // only runs when products/searchTerm change
return products.filter(p =>
p.name.toLowerCase().includes(searchTerm.toLowerCase())
);
}, [products, searchTerm]);
return (
<ul>
{filteredProducts.map(p => <li key={p.id}>{p.name}</li>)}
</ul>
);
}
Without useMemo, this filtering would run on every render — even when an unrelated piece of state (like a modal opening/closing) changed. useMemo "remembers" the result as long as the dependencies stay the same.
function Parent() {
const [count, setCount] = useState(0);
const handleClick = useCallback(() => {
console.log('clicked');
}, []); // empty deps = the same function reference on every render
return <ExpensiveChild onClick={handleClick} />;
}
const ExpensiveChild = React.memo(function ExpensiveChild({ onClick }) {
console.log('ExpensiveChild rendered');
return <button onClick={onClick}>Click</button>;
});
Without useCallback, handleClick would be a new function object on every render — causing the React.memo-wrapped ExpensiveChild to re-render unnecessarily (since its prop "looks" different). useCallback keeps the reference stable.
These hooks aren't free either — they have their own memory and comparison cost. Only use them when:
React.memo child, or is a dependency of another hook.Wrapping every small calculation in useMemo doesn't improve performance — it just makes the code more complex.
useMemo — caches a computed value.useCallback — caches a function reference.What problem React actually solves, how JSX syntax works, and how to build your first component.
Set up a modern React project in seconds with Vite, and understand the resulting folder structure.
Build functional components and pass data from parent to child components using props.