Hackforge Academy

Category: react

What Does useContext Solve?

Published on 27 Oct 2026

Explanation

Context lets a component read a value provided by an ancestor without passing it through every intermediate component. It is often suitable for theme, locale, or current-user information.

Code:

const ThemeContext = createContext('light');

function App() {
  return (
    <ThemeContext.Provider value="dark">
      <Toolbar />
    </ThemeContext.Provider>
  );
}

function Toolbar() {
  const theme = useContext(ThemeContext);
  return <div className={theme}>Toolbar</div>;
}

Explanation

Redux provides a centralized state store, explicit update logic, middleware, and strong developer tooling. Redux Toolkit is the recommended approach for modern Redux applications.

Code:

// Redux Toolkit slice example
const counterSlice = createSlice({
  name: 'counter',
  initialState: { value: 0 },
  reducers: {
    increment: (state) => { state.value += 1; }
  }
});

export const { increment } = counterSlice.actions;

Explanation

Context is built into React and works well for values that change less frequently. Redux is useful when application state has complex transitions, many consumers, middleware needs, or a need for centralized debugging.

Code:

// Context: useful for a shared theme
const ThemeContext = createContext(null);

// Redux: useful for larger, event-driven application state
// Create a store with configureStore from @reduxjs/toolkit.

Explanation

Context transports a value through the component tree, but it does not by itself provide reducers, action history, middleware, or selective subscriptions. You can combine Context with useReducer for a lightweight state solution.

Code:

const CartContext = createContext(null);

function CartProvider({ children }) {
  const [cart, dispatch] = useReducer(cartReducer, []);
  return (
    <CartContext.Provider value={{ cart, dispatch }}>
      {children}
    </CartContext.Provider>
  );
}

Explanation

Shared state alone does not automatically require Redux. Start with component state and Context where appropriate, then introduce a dedicated store when state complexity, tooling, or performance needs justify it.

Code:

function App() {
  const [locale, setLocale] = useState('en');
  return (
    <LocaleContext.Provider value={{ locale, setLocale }}>
      <Page />
    </LocaleContext.Provider>
  );
}

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community