Implementing Global State
Implementing Global State
(useContext)
Topics
Introduction
What is Global State in React
What is the need for a global state
Understanding useContext() hoo
Working useContext() hook
Introduction
Global state management is crucial to building robust and scalable React applications. While various state
management solutions are available, the useContext hook provides a simple and elegant way to handle the
global state. In this module, we will explore implementing global state management using useContext and how
it can streamline your React application's state management.
The Context API allows you to create a context that provides a way to pass data through the component tree
without having to pass props manually at every level.
Global state in React becomes necessary in various scenarios where you need to share and manage state
across multiple components. Here are some common reasons why the global state might be needed
Sharing Data - When data needs to be accessed by multiple components at different levels of the
component tree
Avoiding Prop Drilling - To avoid passing data through many layers of components via props, which can
make the code harder to maintai
Centralized State Management - For a cleaner and more organized codebase by centralizing state logic in
one location
Dynamic Update - When changes in one part of the application should dynamically update other parts
Decoupling components - To make components more independent and focused on their specific
responsibilities
Simplifying State Management - To simplify the process of reading and updating the state consistently.
Let's break down the useContext hook and understand how it works
Context - Context provides a way to pass data through the component tree without having to pass props
down manually at every level. It's particularly useful for sharing global state or configuration settings in an
application
Creating a Context - Use the createContext function to create a new context
Providing Context Value - Wrap the part of your component tree that needs access to the context with a
Context.Provide
Consuming Context Value - Components that need access to the context value can use either the
Context.Consumer component or the useContext hook.
Now that we understand something about the Global state and the useContext() Hook, Let's dive deep into
understanding the workings of the useContext hook by creating a simple theme toggle example in React. In this
example, we'll use the useContext hook to toggle between light and dark themes.
Note - create a react app first with the command “npx create-next-app@latest” before following the steps
given below.
First, create a new file called ThemeContext.js where you define your theme context using the createContext
function:
i.e
ThemeContext.js
Create another file called ThemeProvider.js where you define the ThemeProvider component. This component
will provide the theme and toggle function using the useState hook:
i.e
ThemeProvider.js
};
return (
{children}
</ThemeContext.Provider>
);
};
In your main App.js file, use the ThemeProvider to wrap your application and provide the theme context to its
children:
i.e
App.js
// App.js
import './App.css';
return (
<ThemeProvider>
<ThemedComponent />
</ThemeProvider>
);
};
Now, create a component called ThemedComponent.js that consumes the theme using the useContext hook:
i.e
ThemedComponent.js
// ThemedComponent.js
return (
</div>
);
};
Create a simple CSS file (e.g., styles.css) to style your themed component:
App.css
.app {
text-align: center;
padding: 50px;
.light-theme {
background-color: #f8f8f8;
color: #333;
.dark-theme {
background-color: #333;
color: #fff;
/* styles.css */
.app {
text-align: center;
padding: 50px;
.light-theme {
background-color: #f8f8f8;
color: #333;
.dark-theme {
background-color: #333;
color: #fff;
App.js
import './App.css';
return (
<ThemeProvider>
<ThemedComponent />
</ThemeProvider>
);
};
Start your React application, and you should see a themed component that toggles between light and dark
themes when you click the "Toggle Theme" button.
npm start
Browser output -
The above example demonstrates how the useContext hook is used to consume the theme context in the
ThemedComponent without the need for prop drilling, making the code clean and maintainable.