Introduction to React Context API | Complete React Course in Hindi #57

3 min read 12 hours ago
Published on Feb 07, 2026 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will introduce you to the React Context API, a powerful feature for managing state across your React applications without the need for prop drilling. Understanding how to use the Context API effectively will enhance your ability to build scalable and maintainable React applications.

Step 1: Understanding Context API

  • Context API is used to share data across components without passing props explicitly through every level of the component tree.
  • It is helpful for global state management, such as user authentication status, theme settings, or language preferences.

Key Concepts

  • Provider: A component that provides the state to its children.
  • Consumer: A component that subscribes to context changes to access the state.

Step 2: Creating a Context

  1. Import createContext from React:
    import React, { createContext } from 'react';
    
  2. Create the Context:
    const MyContext = createContext();
    

Step 3: Setting Up a Provider

  1. Create a Provider Component:
    • This component will wrap your application or any subtree that needs access to the context.
    const MyProvider = ({ children }) => {
        const sharedState = { /* your shared state here */ };
        return (
            <MyContext.Provider value={sharedState}>
                {children}
            </MyContext.Provider>
        );
    };
    
  2. Wrap Your Application with the Provider:
    • In your main application file (e.g., App.js), wrap your components with the MyProvider.
    import MyProvider from './path/to/MyProvider';
    
    function App() {
        return (
            <MyProvider>
                <YourComponent />
            </MyProvider>
        );
    }
    

Step 4: Consuming the Context

  1. Import useContext:
    import React, { useContext } from 'react';
    
  2. Access the Context in a Component:
    const YourComponent = () => {
        const contextValue = useContext(MyContext);
        return (
            <div>
                {/* Use contextValue here */}
            </div>
        );
    };
    

Step 5: Updating Context State

  • To update the context state, you can pass functions from the provider to the context value.
  • Example:
    const MyProvider = ({ children }) => {
        const [state, setState] = useState(initialState);
        
        const updateState = (newValue) => {
            setState(newValue);
        };
        
        return (
            <MyContext.Provider value={{ state, updateState }}>
                {children}
            </MyContext.Provider>
        );
    };
    

Common Pitfalls

  • Be cautious with rendering performance; avoid excessive re-renders by minimizing the context's value changes.
  • Ensure that the Provider wraps all components that need access to the context.

Conclusion

The React Context API is a powerful tool for managing global state in your applications. By following these steps, you can create a context, provide it to your components, and consume it effectively. As you build more complex applications, consider combining Context API with other state management solutions like Redux for larger applications. Next, explore the advanced features of Context API and how it can integrate with other React hooks for more dynamic applications.