我有一个静态聊天机器人,可以通过以下方式显示消息:
<ChatMessage bot={true}>Hi.</ChatMessage>
像这样的img:
const ChatBot = () => {
return (
<Styled.ChatBox>
<ChatMessage bot={true}>Hi.</ChatMessage>
<ChatMessage bot={false}>Hello.</ChatMessage>
</Styled.ChatBox>
);
};
这是我的聊天机器人:
function ChatMessage(props) {
return (
<Styled.ChatMessage bot={props.bot}>{props.children}</Styled.ChatMessage>
);
}
ChatMessage.defaultProps = {
bot: false,
};
const Chat = props => {
console.log(props.children);
return (
<Styled.ChatBox>
<Styled.ChatHeader />
<Styled.ChatLog>{props.children}</Styled.ChatLog>
<Styled.ChatInput>
<textarea placeholder="aaaaa ..." rows={4} />
<button>Send</button>
</Styled.ChatInput>
</Styled.ChatBox>
);
};
但是我想知道如何使它动态地显示与文本区域中键入的内容相应的消息,并因此调用一些函数来检查键入的内容并返回响应。但我不知道如何解决这种情况。基本上我需要在聊天中显示用户输入的消息并将该消息发送到我的后端(或前端的某些功能),然后该功能将向我发送响应。