useContext

useContext

Intermediatereact1 min read
contextreact contextcontext apiuse contextcontext consumer

Definition

A React hook for consuming values from a Context — the global state sharing mechanism in React.

خطاف React لاستهلاك القيم من Context — آلية مشاركة الحالة العالمية في React.

Why It Matters

Every component on 404Fault that needs the user's auth state calls `const { user, profile } = useAuth()`. Every component that renders text in two languages calls `const { lang, isRTL, t } = useLanguage()`. Both are useContext-powered hooks.

كل مكوّن في 404Fault يحتاج حالة مصادقة المستخدم يستدعي `const { user, profile } = useAuth()`. وكل مكوّن يُصيّر نصًا بلغتين يستدعي `const { lang, isRTL, t } = useLanguage()`. كلاهما خطافات مدعومة بـuseContext.

Full Definition

useContext subscribes a component to a React Context value created with `createContext`. When the context value changes, all consumers re-render. 404Fault uses two main contexts: `AuthContext` (user, profile, loading) via `useAuth()`, and `LanguageContext` (lang, isRTL, t, setLang) via `useLanguage()`. Context avoids 'prop drilling' — passing props through many component layers.
useContext يشترك بمكوّن في قيمة React Context المُنشأة بـ`createContext`. عند تغيير قيمة الـcontext، تُعاد رسم جميع المستهلكين. 404Fault يستخدم سياقين رئيسيين: `AuthContext` عبر `useAuth()` و`LanguageContext` عبر `useLanguage()`.

Example Usage

// Inside any component: const { user, profile, loading } = useAuth(); const { lang, isRTL, t } = useLanguage();

// داخل أي مكوّن: const { user, profile, loading } = useAuth(); const { lang, isRTL, t } = useLanguage();

Knowledge Graph

Avoid these mistakes when using useContext:

1

Putting too much in a single context — it re-renders all consumers on any change

2

Not wrapping the app in the Provider — consumers fail silently with the default value

Sign in to unlock guided AI explanations from AI Teacher.

Generate a Prompt

Copy this prompt and use it directly with any AI model — no setup needed.

Ready-to-Use Prompt
Help me build a project using useContext.

Explain:
1. What is useContext and why it matters
2. The core architecture and required tools
3. Step-by-step implementation plan
4. Common mistakes to avoid: Putting too much in a single context — it re-renders all consumers on any change, Not wrapping the app in the Provider — consumers fail silently with the default value
5. Best practices and production tips

Official Resources