[英]React Js Hooks / props.Children on chatbot
Hello I had the following logic to make a simple chat bot, but for some reason I am not able to display the messages:您好,我有以下逻辑来制作一个简单的聊天机器人,但由于某种原因我无法显示消息:
const ChatBot = () => {
return (
<Styled.ChatBox>
<ChatMessage bot={true}>Hi.</ChatMessage>
<ChatMessage bot={false}>Hello.</ChatMessage>
</Styled.ChatBox>
);
};
and this is my chatbot:这是我的聊天机器人:
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>
);
};
I'm really not able to identify what I did wrong and I don't know if I did it in the best way or with the best logic I have a div made with styled comp which will be where the messages will be displayed according to the prop bot and I will have the message, but for some reason I am not able to display the message.我真的无法确定我做错了什么,我不知道我是用最好的方式还是用最好的逻辑做的道具机器人和我都会收到消息,但由于某种原因我无法显示消息。 example on: https://codesandbox.io/s/eager-torvalds-fyi77
例如: https : //codesandbox.io/s/eager-torvalds-fyi77
You aren't exporting ChatBot
from src/chatbot/index.js
, you are actually exporting Chat
:您不是从
src/chatbot/index.js
导出ChatBot
,您实际上是在导出Chat
:
import React from "react";
import { Chat, ChatMessage } from "./chat";
const ChatBot = () => {
return (
<Chat>
<ChatMessage bot={true}>Hi.</ChatMessage>
<ChatMessage bot={false}>Hello.</ChatMessage>
</Chat>
);
};
//export default Chat; // You're just exporting what you imported
export default ChatBot; // This is correct
If you change that it should work.如果你改变它应该工作。 See the example below.
请参阅下面的示例。 You will need to run the snippet in fullscreen since the messages will appear off-screen due to your styling.
您需要全屏运行代码片段,因为由于您的样式,消息将显示在屏幕外。
const {useState, useEffect, Fragment} = React; const {css} = styled; const messageBot = css` align-self: flex-start; background-color: #e0e0e0; `; const messageClient = css` align-self: flex-end; background-color: #ffffff; `; const ChatBox = styled.div` position: absolute; position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; width: 22em; height: 30em; border-radius: 6px; background-color: red; `; const ChatLog = styled.div` display: flex; flex-direction: column; width: 100%; overflow: auto; flex: 1 100%; background: blue; `; const ChatMessage = styled.div` margin: 1ex; padding: 1ex; border-radius: 2px; ${props => (props.bot ? messageBot : messageClient)} `; const ChatInput = styled.div` display: flex; padding: 1ex; max-height: 60px; height: 100%; background: yellow; border-bottom-left-radius: 6px; border-bottom-right-radius: 6px; flex: none; & > textarea { width: 100%; border: none; box-sizing: border-box; height: 60px; padding: 20px; flex: 1 1; resize: none; } `; const ChatHeader = styled.div` border-top-left-radius: 6px; border-top-right-radius: 6px; flex: none; background: green; height: 30px; `; const Styled = { ChatBox, ChatLog, ChatHeader, ChatInput, ChatMessage }; function ChatMessageCmp(props) { return ( <Styled.ChatMessage bot={props.bot}>{props.children}</Styled.ChatMessage> ); } ChatMessageCmp.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> ); }; const ChatBot = () => { return ( <Chat> <ChatMessageCmp bot={true}>Hi.</ChatMessageCmp> <ChatMessageCmp bot={false}>Hello.</ChatMessageCmp> </Chat> ); }; function App() { return <ChatBot />; } ReactDOM.render(<App />, document.getElementById("app"));
.App { font-family: sans-serif; text-align: center; } body { padding: 0; margin: 0; }
<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script> <script src="https://unpkg.com/styled-components@4.4.1/dist/styled-components.min.js"></script> <div id="app"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.