
No Comments Yet
Be the first to share your thoughts and start the conversation.
We didn’t really define state management. So just to do some formalities —
State management is like keeping track of everything happening in your application.
Imagine you're playing a game and need to remember your score, level, and items collected. State management is like having a scoreboard and inventory list to track all this information so you can play smoothly. It's about organizing and updating data so your app knows what's happening and can react accordingly.
There are two types of state management
It typically involves managing the data within a single component or module of an application. This local state is specific to that component and is not directly accessible or modifiable by other parts of the application unless explicitly passed down as props or through other means.
For example, (everyone’s favorite)
import React, { useState } from "react";
const Counter = () => {
// Local state variable 'count'
const [count, setCount] = useState(0);
// Update local state 'count'
const increment = () => {
setCount(count + 1);
};
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
};
export default Counter;
On the other hand, we have,
It involves managing data that needs to be accessed and modified by multiple components across the application. This global state is typically stored in a centralized location, such as a global store or context, where it can be accessed and modified by any component that needs it without the need for prop drilling or passing data through multiple layers of components.
Here is an example of global state management using Context API
import React, { createContext, useContext, useState } from "react";
// Create a global context
const CounterContext = createContext();
// Create a provider to manage global state
const CounterProvider = ({ children }) => {
// Global state variable 'count'
const [count, setCount] = useState(0);
// Update global state 'count'
const increment = () => {
setCount(count + 1);
};
return (
<CounterContext.Provider value={{ count, increment }}>
{children}
</CounterContext.Provider>
);
};
// Custom hook to access global state and updater
const useCounter = () => {
return useContext(CounterContext);
};
export { CounterProvider, useCounter };
Next, we import it into the main starting file of the application, wrapping everything inside it. This makes the provider the main parent of the application.
import Counter from "./Counter";
import { CounterProvider } from "./CounterProvider";
const App = () => {
return (
<CounterProvider>
<div className='App'>
<h1>Counter App</h1>
<Counter />
</div>
</CounterProvider>
);
};
export default App;
And then utilize or access it in any component whose parent is CounterProvider.
import { useCounter } from "./CounterProvider";
// Component using global state
const Counter = () => {
// Access global state and updater
const { count, increment } = useCounter();
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
};
But using Context API isn’t the only solution in React.js. We have many options. Out of which, few have really grabbed industries’ attention by the way they solve some unique problems related to global state management.
💡 Check out this must-read documentation on Context API by the React team.
It's important to understand that there isn't a single "best" library among the others. Each one offers a unique approach to state management and addresses different problems more effectively.
Rather than debating with developers on Twitter, consider your specific problem and choose the library that best suits your needs.
So, to summarize, the main difference between these types is in the scope of the data being managed.
Local state management deals with data specific to a single component, while global state management deals with data that needs to be shared and accessed by multiple components throughout the application.
Depending on your need, make a choice!
Be the first to share your thoughts and start the conversation.
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
State management is crucial for keeping track of everything happening in an application. It can be likened to tracking scores and inventory in a video game, allowing the app to react appropriately to changes. In this lesson, we explore the two types of state management: local and global, and discuss various libraries available in React for managing state effectively.
useState
hook.00:00:02 Now that you've reached that paradigm shift where you've been first asking yourself whether you need to implement state management in the first place,
00:00:09 let's discuss some different types of state management.
00:00:12 In simple words, state management is like keeping track of everything happening in your application.
00:00:17 A very good example of that is playing a video game where you need to remember your score, your level, the items you collected.
00:00:24 It's like having the entire inventory list to track all of this information so you can play smoothly.
00:00:30 Primarily, it's about organizing and updating the data so your app knows what's happening and can react accordingly.
00:00:38 We have two different types of state management, local and global.
00:00:44 When talking about the local state management, this means managing data within a single component or module of an application.
00:00:52 This means that the component is not directly accessible by other parts of the application unless we explicitly pass down the props of that component.
00:01:03 For example, everyone's favorite counter example, where we have a single counter component that manages its own state.
00:01:12 Yep, a single useState hook still counts as state management.
00:01:16 Doesn't have to be Redux or Zestand or something more complex.
00:01:20 Technically, we're talking about local state management here.
00:01:24 As this component is encapsulating this state, with the setter function, and at the end of the day, it ends up using it.
00:01:31 On the other hand, there's the global state management, which involves managing data that needs to be accessed and modified by multiple components across
00:01:41 the entire application.
00:01:43 This global state is typically stored in a centralized location.
00:01:47 such as a global store or context, where it can be accessed and modified by any component that needs it without the need for prop drilling or passing data
00:01:57 through multiple layers of components.
00:01:59 An example of that would be good old context API, where we have a counter provider, we create a new React context, we manage the state there,
00:02:11 and then we pass it over to the counter context, which then allows every other component in our application to access it.
00:02:18 How do we access it?
00:02:20 Well, we wrap those components with a counter provider and we can use the counter component.
00:02:25 In the same way, it can be used anywhere.
00:02:28 just by using the use counter hook.
00:02:30 But using the Context API isn't the only solution in React.
00:02:34 There are many options, like Redux, which is very complex, Zustand, which aims for simplicity, Context API, Recoil, and even MobX.
00:02:44 But it's important to understand that there isn't a single best library among the others.
00:02:50 Each one offers a unique approach to state management and addresses different problems effectively.
00:02:56 So rather than debating with other developers on Twitter, consider your specific problem and architect the solution by using the library that works best
00:03:06 for you.
00:03:07 So to summarize, we have local state management and global state management and different libraries that allow us to handle it.