Welcome to the final chapter of this tutorial series!
In Chapter 4: UI Overlay Rendering, we learned how to let our external logic "speak" to the user by drawing graphics on the screen.
However, showing a suggestion is only half the battle. If the AI suggests a better way to phrase a message, shouldn't it be able to fix it for you?
In this chapter, we explore Host State Control. This is the mechanism that allows MoreRight to reach into your application and type text, delete messages, or reset the conversation.
Think of your chat application as an airplane.
In previous chapters, the Co-Pilot could only talk to you ("Watch out!") or show you maps. Host State Control connects the Co-Pilot to a second set of flight controls. If you fall asleep or make a dangerous move, the Co-Pilot can grab the stick and steer the plane safely.
To make this happen, we rely on a core principle of React: State Setters.
inputValue tells the hook what is currently written.setInputValue gives the hook the power to change what is written.
When you pass a set... function to the hook, you are effectively handing over a remote control. Even though the logic lives outside your component, it can push buttons that change your component's memory.
As the host application developer, your job is simple: Grant Permission. You do this by passing your state changers into the hook.
This is standard React code. You have a box for text (input) and a list of chats (messages).
// Inside your ChatApp component
const [input, setInput] = useState("");
const [messages, setMessages] = useState([]);
Explanation: setInput and setMessages are the keys to changing the screen.
When using useMoreRight, you must explicitly pass these functions.
const moreRight = useMoreRight({
enabled: true,
// Give read access
inputValue: input,
// Give WRITE access (The "Dual Controls")
setInputValue: setInput,
setMessages: setMessages,
// ... other props
});
Explanation: By passing setInputValue: setInput, you promise the hook: "If you call this function, my text box will update."
You don't write the code that changes the text; the MoreRight internal logic does. However, for you to trust it, you need to understand what it does.
Imagine the internal logic looks like this:
// Hypothetical Internal Logic inside the hook
if (userText.includes("bad word")) {
// The hook calls the function you gave it!
args.setInputValue("****");
}
How does an external library change variables inside your component?
Let's look at useMoreRight.tsx one last time to see how these controls are defined.
// Inside useMoreRight.tsx
export function useMoreRight(_args: {
// ...
// The Contract: The App MUST provide these functions
setMessages: (action: any) => void;
setInputValue: (s: string) => void;
// ...
})
Explanation:
The TypeScript definition enforces that setInputValue must be a function that accepts a string. This ensures the hook knows exactly how to talk to your app.
setMessages worksControl isn't limited to the input box. The hook can also modify the message history.
// Hypothetical usage inside the hook
const onReset = () => {
// Clear all history
args.setMessages([]);
};
This is powerful! It allows the external logic to:
Throughout this tutorial series, we have built a complete system where an external "Guest" logic can safely integrate with a "Host" chat application.
Host State Control is the final piece of the puzzle. It transforms your application from a passive display into an active, intelligent partner. By granting setInputValue and setMessages access, you allow MoreRight to act as a true Co-Pilot, correcting mistakes and managing the safety of the conversation flow.
Congratulations! You have completed the MoreRight beginner tutorial. You now understand how to integrate powerful, safe, and interactive AI logic into any standard chat application.
Generated by Code IQ