reactjs - react Js : prevent line breaks or submit on enter in the text area

标签 reactjs

代码:

const Chat = props => {
  const dispatch = useDispatch();
  const messages = useSelector(state => state.Chat);
  const [text, setText] = React.useState('');

  return (
    <Styled.ChatBox>
      <Styled.ChatHeader>
        <p>Chat Bot</p>
        <div>
          <FontAwesomeIcon icon={faAngleDown} size="1x" color="white" />
          <FontAwesomeIcon icon={faTimes} size="1x" color="white" />
        </div>
      </Styled.ChatHeader>
      <Styled.ChatLog>
        {/* <Styled.MessageWrapper bot={true}>
          <Styled.BotImg src={BotLogo} bot={true} />
          <Styled.ChatMessage bot={true}>
            dsasasasasasasasasasa
          </Styled.ChatMessage>
        </Styled.MessageWrapper> */}
        {messages.map(message => (
          <Styled.MessageWrapper bot={true}>
            <Styled.BotImg src={BotLogo} bot={false} />
            <Styled.ChatMessage bot={true}>{message.text}</Styled.ChatMessage>
          </Styled.MessageWrapper>
        ))}
      </Styled.ChatLog>
      <Styled.ChatInput>
        <textarea
          value={text}
          onChange={e => setText(e.target.value)}
          placeholder="Digite aqui sua mensagem"
        />
        <button onClick={() => dispatch(sendMessage(text))}>
          <FontAwesomeIcon icon={faPaperPlane} size="lg" color="black" />
        </button>
      </Styled.ChatInput>
    </Styled.ChatBox>
  );
};

您好,我很怀疑我将如何不允许在我的文本区域中换行并在按下按钮时清除它 调用我的点击事件时清除文本区域字段

最佳答案

像这样防止 Enter 的默认值:

<textarea onKeyPress={e => {
  if(e.key === 'Enter')
     e.preventDefault()
  }}
/>

关于reactjs - react Js : prevent line breaks or submit on enter in the text area,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60008415/

相关文章:

javascript - React 重新渲染后 Zurb Foundation 6 刷新

javascript - 您提供了 `undefined`,其中 Redux Observable 中预期存在流

reactjs - 从 map 返回的 React 对象传播(ES6)

reactjs - 在react.js中刷新页面后,某些文件(js、css、图像)未加载

javascript - redux-thunk 和 redux-promise 有什么区别?

javascript - 如何检查任何一个变量是否大于0

reactjs - 有没有办法让 `react-scripts` 在构建的根目录中包含额外的文件?

javascript - 如何在此 React 应用程序中设置编辑项目功能?

css - 如何在手机屏幕中设置maxWidth?

reactjs - 如何从 props 中破坏 `data-*` (连字符大小写)属性?