Linn Linn Htun
Linn Linn Htun
AvatarLinn Linn Htun

Understanding State

August 21, 2023

Understanding State

State management is one of the most fundamental concepts in modern frontend development. This post explains what state is in the context of JavaScript applications, the difference between local component state and global application state, common state management challenges, and why dedicated state management libraries like Pinia, Vuex, and Redux were created.

What is state?

The word state is a word that's thrown around a lot in the world of javascript. But what exactly does it mean? State refers to the data in the application. This can be data retrieved from a database or API data like a blog post, product or media content. You can use the state to store application data too. There are certain types of data you wouldn't normally store in a database. However, they are still necessary for your app to function correctly.

For example, a simple dropdown. If a user clicks on a dropdown button, then the contents of that dropdown should be shown. If they click away from that dropdown, then the content should be hidden. This is the type of data you would have to keep track of.

Another example would be keeping track of the page the user is currently on. Are they on the home page or contact page? You would display different content. You provides one way of storing data by adding the data function to a component, while not wrong. It's considered good practice to have one single source of truth. 

All of this is considered the state of the application when you hear the word state. It's another way of saying data. It's data that is carefully kept track of while the application is live on a user's browser. If the user is on the home page, then you can call this the home state. If they're logging in, then you can call this the authentication state. It's used to describe what is currently happening in the application.

So, don't think of state as the data from the database. It's also the data that keeps track of the little things. 

The data property can be considered a way to store the local state its data that only one component needs. However, what if we need to share data across multiple components? We need a way to have global state. 

Global state is when data is shared across multiple components. We have the option of passing down data to other components using prompts. To send data back to the parent component, you could use a callback function or emit and events.

Luckily, Vue allows you to create your own library to manage state on a global level. The most popular solution is called Pinia

Frequently Asked Questions

What is state in a JavaScript application?

State refers to any data in your application that can change over time and affects what is rendered on screen — this includes user input, fetched data, UI states like loading or error, and application-wide data like the logged-in user.

When do you need a state management library?

You need a state management library when multiple unrelated components need to share the same data, when prop drilling becomes unwieldy, or when you need predictable state changes with debugging tools like time-travel debugging.