liveBook · Manning

State management

As explained in Build AI-Enhanced Web Apps

State management is the organization, storage, and updating of application state over time.

State management keeps application data available and consistent as users interact with the UI. In a conversational AI application, that includes the current input, loading status, messages, and the history needed to preserve context. The important design question is where each piece of data lives and how it changes. section 2.5.4 section 8.1.3

A chat application may keep recent messages on the client for quick display while the server retains the full conversation for context and security. On submission, the client adds the new message locally and sends it to the server; the server processes it against its history and returns a response that updates the client. Client-provided history cannot simply be trusted, so the two states need deliberate synchronization. section 4.0

Keep the state flow manageable at each layer:

  • Put interaction state and API calls in a custom React hook rather than crowding the page component. Identify what stays internal and return only what the UI needs; use helper functions when updates grow complex. section 2.5.4
  • Render from state: a chat list maps messages to message components and shows a loading indicator while a response is pending. section 2.5.4
  • For shared AI and UI state, use the Vercel AI SDK’s createAI provider to define initial states and actions, then place the provider around the application layout. Its state-management functions and hooks include getMutableAIState, useActions, useUIState, and useAIState. section 4.2.2
  • For retrievable sessions, persist conversation history and user settings. An interview assistant uses Upstash Redis with programmatically maintained session-ID-to-message pairings; SQL is preferred when persistence needs transactions. section 10.3.1

A chat submission’s state changes

A useChatFormSubmit hook starts with messages, inputValue, and isLoading. Its submit handler trims the input, ignores an empty value, sets loading, clears the input, appends a user message, and requests an assistant response. ChatList receives messages and isLoading to reflect those changes in the interface. section 2.5.4

Listing 4.6 Example creating an AI provider
const AI = createAI({ 
  actions: {    #1
    fetchUserData: async (userId) => {
      const response = await fetch(`/api/users/${userId}`);
      const data = await response.json();
      return data;
    },
    updateUserSettings: async (settings) => {
      await fetch('/api/settings', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(settings),
      });
    }
  },
  initialAIState: {
    userProfile: { name: '', email: '' },
    settings: { theme: 'light', notifications: true },
  },    #2

  initialUIState: {
    isSidebarOpen: false,
  },    #3
});
...
const AppLayout = ({ children }) => {
  return (
    <AI>.   #4
      {children}
    </AI>
  )
 }

The createAI example shows actions alongside initial AI and UI states, then wraps the application layout in the returned provider. section 4.2.2

When state behaves unexpectedly, log state changes and component behavior to locate mutations and surprising transitions. Keep AI logic separate from UI components so the data flow is easier to trace and maintain. section 8.1.3

How Build AI-Enhanced Web Apps covers it

How deep it goes
Works through hook-level updates, provider setup, synchronization, persistence choices, and debugging guidance rather than only defining the term.
Examples
Uses a message-rendering chat list, a submission hook, a profile-and-settings provider, and Redis-backed interview sessions.
What it assumes
Assumes familiarity with React components and application code while introducing custom-hook steps and the provider’s key functions.
What you can do after
Build it from scratch with a React hook, or apply it via the Vercel AI SDK’s createAI provider.
How it connects
Connects React hooks, React context, server actions, conversation history, data persistence, and separation of concerns.

Also explained in

Compare how 9 books explain state management →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.