温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.mui.com/x/react-chat/core/examples/controlled-state/
点击访问原文链接

Chat - Controlled state - MUI X

Chat - Controlled state - MUI XSkip to content🚀 Material UI and MUI X v9 are out! Check out the announcement blogpost.Search…

MUI X

Chatv9.0.0-alpha.7What's new in MUI XIntroductionCommon conceptsData GridDate and Time PickersChartsTree ViewSchedulerPreviewChatPreviewMigration + Chat - Controlled state

Drive the public chat models from React state while the runtime keeps normalized internals.

This demo demonstrates the controlled state model — the major public design choice of the core package. All four public state models are owned by the parent component while the runtime still streams, normalizes, and derives selectors internally.

Key conceptsThe four controlled models

Each model has a controlled prop, a change callback, and an uncontrolled default:

Model Controlled prop Change callback Messages messages onMessagesChange Conversations conversations onConversationsChange Active conversation activeConversationId onActiveConversationChange Composer value composerValue onComposerValueChange Wiring controlled state

Pass your React state directly to ChatProvider:

ChatProvider
adapter={adapter}
conversations={conversations}
onConversationsChange={setConversations}
activeConversationId={activeConversationId}
onActiveConversationChange={setActiveConversationId}
messages={messages}
onMessagesChange={setMessages}
composerValue={composerValue}
onComposerValueChange={setComposerValue}
>
ControlledStateChat />
ChatProvider>
CopyCopied(or $keyC) When to use controlled state

Use controlled state when you need to:

sync chat state with a global store (Redux, Zustand, etc.) persist messages across navigation or page reloads drive the conversation list from an external data source coordinate the composer value with external UI (for example, slash commands)

Start with default* props for prototyping and switch to controlled when the need arises — no other changes are required.

Controlled headless stateproductSupportProductMessages

2

Conversations

2

Active

product

Composer

Document the controlled models.

MUI Agent

The controlled API keeps public state array-first.

Sam

That is the behavior we want to document.

Key takeaways Controlled state lets you own the source of truth while the runtime still handles streaming and normalization You can switch from uncontrolled to controlled at any time without changing the runtime model The onMessagesChange callback fires with the full array after every update, including streaming deltas See also State and store for the full ChatProvider props reference Selector-driven thread for efficient rendering with controlled state Conversation history for adapter-driven conversation loading API ChatRoot Edit this page

Was this page helpful?

Blog

Store

Contents

Key conceptsThe four controlled modelsWiring controlled stateWhen to use controlled stateKey takeawaysSee alsoAPIBecome a Diamond sponsorMUI stands in solidarity with Ukraine.Search…

Chat - Controlled state - MUI X,AI智能索引,全网链接索引,智能导航,网页索引

    Drive the public chat models from React state while the runtime keeps normalized internals.