Search Knowledge

© 2026 LIBREUNI PROJECT

Modern JavaScript & Ecosystem / Frameworks: React

Advanced React: Context and Optimization

Scaling Component Architecture

As applications grow, passing props through multiple levels of components (known as “Prop Drilling”) becomes unmaintainable. React provides patterns and APIs to handle global cross-cutting concerns and to optimize rendering performance.

The Context API

Context provides a way to share values between components without having to explicitly pass a prop through every level of the tree. It is designed for “global” data like the current user, theme, or preferred language.

Components of Context

  1. createContext: Initializes the context object.
  2. Provider: A component that wraps part of your app to make the context value available to its descendants.
  3. useContext: A hook used by consumer components to access the current context value.

Performance Note: Every time the Provider value changes, all components that consume that context will re-render. Context should not be used as a high-frequency state management tool (like for real-time mouse positions or animations).

Optimization Hooks: memo, useMemo, and useCallback

React is generally fast, but unnecessary re-renders in large trees can lead to “jank” (visual stutter).

React.memo

A Higher-Order Component that wraps a functional component. It performs a shallow comparison of props; if the props are identical to the last render, React skips re-rendering the component and reuses the last rendered result.

useMemo

Returns a memoized value. It is used to cache the result of expensive computations so they aren’t recalculated on every render unless their dependencies change.

useCallback

Returns a memoized function. This is particularly important when passing callbacks to memoized child components. In JavaScript, functions are objects, so a function defined inside a component is “re-created” on every render (forming a new reference). useCallback ensures the function reference stays the same.

Component Composition vs. Inheritance

React strongly advocates for Composition over Inheritance. Instead of building complex hierarchies where classes inherit behavior from parents, you build components by combining smaller, specific components.

Containment

Using the children prop to pass elements into a component without the component needing to know what those elements are.

Specialization

Creating a “generic” component (e.g., Dialog) and then creating specialized versions (e.g., WelcomeDialog) that configure the generic one with specific props.

When should you reach for useMemo?

Summary of Optimization Patterns

  1. Keep State Local: Don’t lift state higher than necessary.
  2. Memoize Components: Use React.memo for expensive leaf components.
  3. Optimize Callbacks: Pair useCallback with React.memo to prevent cascading re-renders.
  4. Virtualization: For extremely large lists, use libraries like react-window to render only the items currently visible on the screen.