【问题标题】:React Js Hooks / props.Children on chatbotReact Js Hooks / props.Children on chatbot
【发布时间】:2020-01-31 02:14:55
【问题描述】:

您好,我有以下逻辑来制作一个简单的聊天机器人,但由于某种原因我无法显示消息:

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>
  );
};

我真的无法确定我做错了什么 我不知道我是否以最好的方式或最好的逻辑做到了 我有一个用样式化的 comp 制作的 div 这将是根据道具机器人显示消息的位置 我会收到消息,但由于某种原因我无法显示消息。 示例: https://codesandbox.io/s/eager-torvalds-fyi77

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    实际上,您的chatbot/index.js 文件中缺少export default ChatBot

    【讨论】:

    【解决方案2】:

    您不是从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
    

    如果你改变它应该可以工作。请参见下面的示例。您需要全屏运行 sn-p,因为由于您的样式,消息将显示在屏幕外。

    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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 2019-08-15
      • 2017-08-28
      • 2018-07-15
      • 1970-01-01
      • 2017-02-01
      • 2020-12-31
      相关资源
      最近更新 更多