Custom Components with useEffect
Learn to build custom React components in Upstreet Agents, leveraging the useEffect hook for AI-driven interactions.
useEffect
is a React Hook that lets you synchronize a component with an external system.
Explanation
- Manages side effects in functional components.
- Executes after the component renders and re-runs when dependencies change.
Usecase
- Connecting to an external system
- Wrapping Effects in custom Hooks
- Controlling a non-React widget
- Fetching data with Effects
- Specifying reactive dependencies
- Updating state based on previous state from an Effect
Step 1: Integrate useEffect with Upstreet agents to build a component that generate random names
-
Install the Upstreet SDK using
usdk create
-
Navigate to the
agent.tsx
file. -
You will find a pre-built
MyAgent
component like that.
In this example useState
and useEffect
both are being utilized.
Step 2: Import dependencies and create SimpleAgentEffect
component
Now create a component named SimpleAgentEffect
and set your states using useState
After defining the useState, the useEffect is being used to display a greeting from the agent and update the message state
This code runs after the component renders, triggering the agent to say "Hello! I'm here to assist you,"
and updates the message
state to indicate the greeting was sent, with the effect re-running whenever the agent
changes.
Step 3: Modify the SimpleAgentEffect
component
Give agent a customized Prompt
Display the message here
The final state of SimpleAgentEffect
look like this.
Step 4: Import SimpleAgentEffect
in the Agent
Components
Step 5: Test the agent
Run usdk chat
in the terminal to test the agent.
As soon as the agent renders, it will greet you.