You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+168-5Lines changed: 168 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6132,16 +6132,68 @@ Technically it is possible to write nested function components but it is not sug
6132
6132
6133
6133
**[⬆ Back to Top](#table-of-contents)**
6134
6134
6135
-
267. ### How does context works using useContext hook?
6135
+
267. ### What is useContext? What are the steps to follow for useContext?
6136
+
The `useContext` hook is a built-in React Hook that lets you access the value of a context inside a functional component without needing to wrap it in a <Context.Consumer> component.
6137
+
6138
+
It helps you **avoid prop drilling** (passing props through multiple levels) by allowing components to access shared data like themes, authentication status, or user preferences.
6139
+
6140
+
The usage of useContext involves three main steps:
6141
+
#### **Step 1 : Create the Context**
6142
+
6143
+
Use `React.createContext()` to create a context object.
6144
+
6145
+
```jsx
6146
+
importReact, { createContext } from'react';
6147
+
6148
+
constThemeContext=createContext(); // default value optional
6149
+
```
6150
+
6151
+
You typically export this so other components can import it.
6152
+
6153
+
#### **Step 2: Provide the Context Value**
6154
+
6155
+
Wrap your component tree (or a part of it) with the `Context.Provider` and pass a `value` prop.
6156
+
6157
+
```jsx
6158
+
functionApp() {
6159
+
return (
6160
+
<ThemeContext.Provider value="dark">
6161
+
<MyComponent />
6162
+
</ThemeContext.Provider>
6163
+
);
6164
+
}
6165
+
```
6166
+
6167
+
Now any component inside `<ThemeContext.Provider>` can access the context value.
6168
+
6169
+
#### **Step 3: Consume the Context with** `**useContext**`
6170
+
6171
+
In any functional component **inside the Provider**, use the `useContext` hook:
268. ### What are the use cases of useContext hook?
6140
-
Some of the common use cases of useContext are listed below,
6184
+
The `useContext` hook in React is used to share data across components without having to pass props manually through each level. Here are some common and effective use cases:
6141
6185
6142
-
1. **Theme customizations:** The useContext hook can be used to manage and apply custom themes for an application. That means it allows users to personalize the appearance of the application.
6143
-
2. **Support localization:** The context hook is helpful to implement localization by providing translated strings to components based on the user's language/locale preference.
6144
-
3. **User authentication:** It can be used to manage user authentication or session status and display user specific information with in components.
6186
+
1. **Theme Customization**
6187
+
`useContext` can be used to manage application-wide themes, such as light and dark modes, ensuring consistent styling and enabling user-driven customization.
6188
+
2. **Localization and Internationalization**
6189
+
It supports localization by providing translated strings or locale-specific content to components, adapting the application for users in different regions.
6190
+
3. **User Authentication and Session Management**
6191
+
`useContext` allows global access to authentication status and user data. This enables conditional rendering of components and helps manage protected routes or user-specific UI elements.
6192
+
4. **Shared Modal or Sidebar Visibility**
6193
+
It's ideal for managing the visibility of shared UI components like modals, drawers, or sidebars, especially when their state needs to be controlled from various parts of the app.
6194
+
5. **Combining with** `**useReducer**` **for Global State Management**
6195
+
When combined with `useReducer`, `useContext` becomes a powerful tool for managing more complex global state logic. This pattern helps maintain cleaner, scalable state logic without introducing external libraries like Redux.
6196
+
Some of the common use cases of useContext are listed below,
6145
6197
6146
6198
**[⬆ Back to Top](#table-of-contents)**
6147
6199
@@ -6581,6 +6633,117 @@ Technically it is possible to write nested function components but it is not sug
6581
6633
```
6582
6634
6583
6635
**[⬆ Back to Top](#table-of-contents)**
6636
+
6637
+
283. ### How does useContext works? Explain with an example
6638
+
The `useContext` hook can be used for authentication state management across multiple components and pages in a React application.
6639
+
6640
+
Let's build a simple authentication flow with:
6641
+
6642
+
* **Login and Logout buttons**
6643
+
* Global `AuthContext` to share state
6644
+
* Components that can **access and update** auth status
6645
+
6646
+
**1. Create the Auth Context:**
6647
+
6648
+
You can define `AuthProvider` which holds and provides `user`, `login()`, and `logout()` via context.
0 commit comments