Welcome to the claudeCode codebase! If you are new here, you might be wondering: How does the application remember what I just asked it to do?
That is where State Management comes in. It is the very first concept we need to understand because it powers everything else.
Imagine you are cooking a complex meal. You have a mental list of ingredients you need, steps you've finished, and what is currently in the oven. That "mental list" is your State.
If you walked out of the kitchen and immediately forgot everything (the oven temp, the chopped onions, the recipe), disaster would strike!
In claudeCode, State Management acts as the application's brain. It is a central place (a "Store") that holds all the important information while the program is running.
Let's look at a simple scenario:
You ask claudeCode to: "Fix the bug in server.js."
To succeed, the application needs to store three things immediately:
server.js."Without State Management, the moment the AI tried to open the file, it would forget why it opened it!
We keep our state organized in a "Store." You can think of the Store as a giant JavaScript object that looks like this:
// A simplified view of the Global Store
{
task: "Fix bug in server.js",
isThinking: true,
permissions: {
canEditFiles: true,
canRunCommands: false
},
activeTeammates: []
}
We don't keep data scattered inside different files. We keep it all in one place. This way, if the Permission System changes a setting, the UI knows about it instantly.
Because the application is big, we group data into categories (or "slices").
In our code, we use special functions (often called "hooks") to read from and write to the store.
Here is how a component asks for the current task description.
import { useTaskState } from './state/store';
function TaskDisplay() {
// Read the current description from the store
const { description } = useTaskState();
return description; // Output: "Fix bug in server.js"
}
Explanation: We simply call useTaskState to grab the data we need. If the task changes, this variable updates automatically.
When the user gives a new command, we need to update the store.
import { setTaskDescription } from './state/actions';
function updateMission(newMission) {
// Check if mission is valid
if (newMission) {
// This updates the Global Store immediately
setTaskDescription(newMission);
}
}
Explanation: We don't change variables directly. We call an "Action" function (setTaskDescription). This ensures the data is saved safely.
What happens when you change the state? It's a cycle.
Here is a visual flow of updating a permission setting:
Deep inside the state/ directory, we define exactly what our state looks like. We use a library (similar to jotai or zustand) to create these data containers.
Here is a simplified look at how a state "atom" (a tiny piece of state) is created.
// state/atoms.ts
import { atom } from 'state-library';
// 1. Define the default state for permissions
const defaultPermissions = {
canEditFiles: false,
canRunShell: false,
};
// 2. Create the state container
export const permissionsAtom = atom(defaultPermissions);
Explanation: We define the starting values. permissionsAtom is now a shared container that can be imported anywhere in the app.
Later, in Chapter 8: Permission & Security System, we will see how these exact atoms are used to block dangerous commands if the state says false.
Almost every feature we will build relies on this chapter:
You have learned that State Management is the memory of claudeCode. It is a single, central box where we keep track of tasks, settings, and permissions. By using "hooks," we can read this data or update it from anywhere in the application.
Now that we have data stored, we need a way to show it to the user.
Next Chapter: Ink UI Framework
Generated by Code IQ