Global State Management in React - Context - API
Global State Management in React - Context - API
Global State
Management
In React (Part 1)
@oluwakemi Oluwadahunsi
Introduction
Global state management in React refers to
managing the state that is shared across multiple
components in an application.
@oluwakemi Oluwadahunsi
Why is Global State Management
Important?
Avoids Prop Drilling:
Prop drilling refers to the process of passing data
down through multiple layers of components just
to get it to the one that needs it. This can make
your code complex and hard to maintain.
Centralized State:
By having a centralized place for your application's
state, you can better manage and organize your
data, making it easier to debug and maintain.
@oluwakemi Oluwadahunsi
How Global State Works in React
(Using Context API)
The React Context API is a feature that helps to
manage state globally across your application
without having to pass props down manually at
every level.
@oluwakemi Oluwadahunsi
The Problem Context API Solves
@oluwakemi Oluwadahunsi
Example of Prop Drilling:
@oluwakemi Oluwadahunsi
When to Use the Context API
When you need to manage global state across
multiple components.
@oluwakemi Oluwadahunsi
Setting Up Global State with the
Context API
Step by step example on how to set up context API
in a React application:
@oluwakemi Oluwadahunsi
Step 2: Creating the Theme Context
context created
default state
@oluwakemi Oluwadahunsi
ThemeContext: This is our context created
using createContext(). It will hold the current
theme and the toggle function.
@oluwakemi Oluwadahunsi
You can also wrap your entire application in your
App.js file like this:
It will also work the same way like the first method.
I only prefer the first method because it makes my
code looks neat, a lot usually go on in the app.js
file already.
@oluwakemi Oluwadahunsi
Create a new file called ThemeSwitcher.js to use
the ThemeContext and add these codes:
@oluwakemi Oluwadahunsi
In this component:
@oluwakemi Oluwadahunsi
Let’s add more components to demonstrate how
the global state is shared across the app.
Create a Header.jsx component:
@oluwakemi Oluwadahunsi
In this example, we have Multiple Components
sharing the Same State. Both the Header and
ThemeSwitcher components can access the theme
state without any prop drilling.
This demonstrates how the Context API enables
global state sharing across different parts of the
app.
Step 5: Adding Some Basic Styling
We can add some simple CSS to make the app look
better. In the index.css or App.css file:
@oluwakemi Oluwadahunsi
CSS styles:
@oluwakemi Oluwadahunsi
You should have this in your browser: Light Mode
Dark Mode:
@oluwakemi Oluwadahunsi
When you click on the button, you toggle the
different modes for both components (Header and
ThemeSwitcher) at once because they both have
access to the Theme Context provider wrapped
around them.
Why Context API is Effective for Global State
Management
Avoids Prop Drilling: By using the Context API,
you don't need to pass props through multiple
levels of components. Any component within
the Provider can access the global state
directly.
@oluwakemi Oluwadahunsi
When you click on the button, you toggle the
different modes for both components (Header and
ThemeSwitcher) at once because they both have
access to the Theme Context provider wrapped
around them.
Why Context API is Effective for Global State
Management
Avoids Prop Drilling: By using the Context API,
you don't need to pass props through multiple
levels of components. Any component within
the Provider can access the global state
directly.
@oluwakemi Oluwadahunsi
Best Practices for Context API in
React
1. Avoid Overuse of Context
While Context API is powerful, it’s not always the
best choice for all state management. Overusing it
for every piece of state can lead to unnecessary
complexity.
@oluwakemi Oluwadahunsi
Stay tuned for Part
2, where we’ll cover
another advanced
topic like combining
the Context API
with Reducers to
handle more
complex state logic.
@oluwakemi Oluwadahunsi
I hope you found this material
useful and helpful.
Remember to:
Like
&
Share with your network, be
helpful to someone 👌
@oluwakemi Oluwadahunsi
Hi There!
Thank you for reading through
Did you enjoy this knowledge?
kodemaven-portfolio.vercel.app