Course contentsShow
← React
Lesson 1476 of 2,39637. React 19 and BeyondFree lesson

use() with Context: An Alternative to useContext

Reading context values with use(MyContext) and when this syntax might be preferred over useContext.

use() with Context: An Alternative to useContext

What you'll learn: How to read context values with use(MyContext) instead of useContext(MyContext), and when this new syntax might be preferred.

The Core Idea

React 19 introduced use() as a way to read context values, providing an alternative to the traditional useContext() hook. While useContext(MyContext) has always worked perfectly fine, use(MyContext) offers the same functionality with one key advantage: it can be called conditionally.

import { use } from 'react';
import { ThemeContext } from './ThemeContext';

function Button({ skipTheming }) {
  // This is allowed with use()!
  const theme = skipTheming ? null : use(ThemeContext);
  
  return (
    <button style={theme ? { color: theme.primaryColor } : {}}>
      Click me
    </button>
  );
}

With traditional useContext(), you'd get an error trying to call it conditionally — hooks must be called at the top level. But use() breaks this rule intentionally, giving you flexibility.

When to Prefer use() Over useContext

Most of the time, stick with useContext() — it's clearer and more familiar. Choose use(MyContext) when:

  1. Conditional context reading: You only need context in certain branches
  2. Consistency: Your component already uses use() for promises, so using it for context too feels uniform

Think of it like having both a screwdriver and a drill — the screwdriver (useContext) works great for most jobs, but sometimes the drill's (use()) extra flexibility is exactly what you need.

Key Takeaway: use(MyContext) reads context values just like useContext(MyContext), but can be called conditionally, making it useful when you need that flexibility — though useContext() remains the standard choice for typical cases.