Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
A component can re-render when its state changes, when its parent renders, or when a consumed context value changes. To reduce unnecessary work, use appropriate state boundaries, stable props, `React.memo`, and memoization only where profiling shows it is useful.
A React component generally re-renders when its own state changes, when its parent renders and React renders that part of the tree again, or when a context value consumed by the component changes. A component can also re-render when its external store subscription reports a relevant update.
A re-render does not necessarily mean that the browser DOM is changed. React executes the component again, creates a new element tree, and then reconciliation determines which actual DOM changes are required.
For example, if a parent passes an object created on every render, a memoized child can still render again:
function Parent({ user }) {
const options = { theme: 'dark' };
return <Child user={user} options={options} />;
}
const Child = React.memo(function Child({ user, options }) {
return <div>{user.name}</div>;
});options has a new reference every time Parent renders, so React.memo cannot skip the child render based on shallow comparison. If the object is expensive or the child is expensive to render, a stable value could be created with useMemo.
However, blindly adding useMemo and useCallback is not an optimization strategy. They introduce their own bookkeeping and can make code harder to understand. A better approach is to keep state local where possible, avoid unnecessary context updates, split large components, use stable props when it matters, and profile the application before optimizing.