DEV Community

Cover image for React Context API: Simplified Explanation with Example
Ankita Kanchan
Ankita Kanchan

Posted on

React Context API: Simplified Explanation with Example

In the world of React.js, managing state between elements can sometimes be a daunting task, especially when dealing with deep elements or passing multilevel objects. This is where the React Context API comes into play. The React Context API provides a way to share information about objects without declaring the objects at each level of the tree. In this article, we will examine the React Context API with easy understand examples to better understand its concepts.

Understanding the React Context API:

At its core, React Context API allows you to create a global state that can be accessed by any component within the component tree, regardless of its level.

It has three main features:

Context: This is where the global state is defined. It serves as a container for the data you want to share across components.

Provider: The Provider component allows you to pass the data (state) down to all the components in the tree. It acts as the source of truth for the data.

Consumer: The Consumer component enables components to consume the data (state) provided by the Provider within the component tree.

Example Scenario:

Let's consider a scenario where a user logs into an application and we want to pass their details (such as username, email, etc.) to various child components after the login process. We can achieve this efficiently using React Context API.

context API flow

In this diagram:

App Component: The top-level component of the application. It wraps child components with the UserProvider.

LoginComponent: Allows users to log in by providing their credentials. On successful login, it updates the user's data using functions provided by UserContext.

UserInfoComponent: Displays user information. It consumes the UserContext to access user data and render the UI accordingly.

UserContext: Context containing user data and functions for managing user authentication (login and logout).

UserProvider: Wraps the entire component tree and provides the UserContext to all child components. It manages the state of the user data and authentication status.

This diagram illustrates the flow of data and functionality through the components using React Context API for managing user authentication and sharing user details. Lets see the Implementation:

1. Implementing User Context:

In our example, we start by creating a UserContext using React's createContext function. This context serves as a container to hold the user's data, such as username, email, etc. Initially, the context has a default value of null because the user is not logged in initially.

We then create a UserProvider component, which is responsible for managing the user's data state and providing it to other components in the application. Inside the UserProvider, we use the useState hook to manage the user's data. We have functions like loginUser and logoutUser to update the user's data. We wrap the children components with UserContext.Provider and pass down the user's data and functions.

// UserContext.js
import React, { createContext, useState } from 'react';

// Create a context with default value (null)
const UserContext = createContext(null);

// Create a provider component
export const UserProvider = ({ children }) => {
  const [user, setUser] = useState(null);

  const loginUser = (userData) => {
    setUser(userData);
  };

  const logoutUser = () => {
    setUser(null);
  };

  return (
    <UserContext.Provider value={{ user, loginUser, logoutUser }}>
      {children}
    </UserContext.Provider>
  );
};

export default UserContext;

Enter fullscreen mode Exit fullscreen mode

2. Login Component:
The LoginComponent is a simple form where users can input their credentials (username and password) and initiate the login process. When the user clicks the login button, the handleLogin function is triggered. In this function, we simulate a login process (e.g., fetching user data from a server) and retrieve the user's details. Once we have the user's details, we call the loginUser function provided by the UserContext to update the user's data.

// LoginComponent.js
import React, { useContext, useState } from 'react';
import UserContext from './UserContext';

const LoginComponent = () => {
  const { loginUser } = useContext(UserContext);
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');

  const handleLogin = () => {
    // Simulate login process and retrieve user data
    const userData = {
      username: username,
      email: 'example@email.com', // Example email
      // Add more user details as needed
    };

    // Login user and pass user data to context
    loginUser(userData);
  };

  return (
    <div>
      <input type="text" placeholder="Username" value={username} onChange={(e) => setUsername(e.target.value)} />
      <input type="password" placeholder="Password" value={password} onChange={(e) => setPassword(e.target.value)} />
      <button onClick={handleLogin}>Login</button>
    </div>
  );
};

export default LoginComponent;

Enter fullscreen mode Exit fullscreen mode

3. Child Component Consuming User Context:
The UserInfoComponent is an example of a child component that consumes the user context. It displays the user's information such as username and email. It uses the useContext hook to access the UserContext and retrieve the user's data. If the user is logged in (user is not null), it displays a welcome message along with the user's details. Otherwise, it prompts the user to log in.

// UserInfoComponent.js
import React, { useContext } from 'react';
import UserContext from './UserContext';

const UserInfoComponent = () => {
  const { user } = useContext(UserContext);

  return (
    <div>
      {user ? (
        <div>
          <h2>Welcome, {user.username}!</h2>
          <p>Email: {user.email}</p>
          {/* Display other user details as needed */}
        </div>
      ) : (
        <p>Please log in to view user info.</p>
      )}
    </div>
  );
};

export default UserInfoComponent;

Enter fullscreen mode Exit fullscreen mode

4. Wrapping Components with Provider:
Finally, in the App component, we wrap our application's components with the UserProvider. This ensures that the UserContext is available to all components within the component tree, allowing them to access the user's data and authentication state.

// App.js
import React from 'react';
import { UserProvider } from './UserContext';
import LoginComponent from './LoginComponent';
import UserInfoComponent from './UserInfoComponent';

const App = () => {
  return (
    <UserProvider>
      <div>
        <h1>User Authentication Example</h1>
        <LoginComponent />
        <UserInfoComponent />
        {/* Other components consuming user context */}
      </div>
    </UserProvider>
  );
};

export default App;

Enter fullscreen mode Exit fullscreen mode

By using React Context API, we can efficiently manage user authentication and access user data across various components in our application without the need for prop drilling. This approach leads to cleaner and more maintainable code, as components can focus on rendering UI based on the provided context rather than handling state management directly.

Conclusion:
React Context API is like the superhero of state management, swooping in to save the day by effortlessly passing data through your component tree without the hassle of prop drilling! With just a few lines of code, you can share global state like a boss, making your application more organized and your codebase cleaner. 🦸‍♂️💥 So, next time you find yourself drowning in a sea of props, remember: React Context API has got your back! 🌊🚀

Top comments (0)