【问题标题】:Unable to get event.stopPropagation(); working within my code无法获取 event.stopPropagation();在我的代码中工作
【发布时间】:2022-12-01 11:49:05
【问题描述】:

我有以下链接到文本区域的代码,但由于某种原因它没有像您预期的那样工作,用户仍然可以在文本区域中键入:

  const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
    const { code, target } = event;
    const { value } = target as HTMLInputElement;

    if (code === 'Enter') {
      if ([...value].filter((word) => word === '\n').length >= DETAIL_MAX_LINES) {
        event.stopPropagation();
      }
    }
  };
<TextField
        onKeyDown={handleKeyDown}
        ...
/>

任何帮助使此功能正常工作的帮助都将非常棒,谢谢!

【问题讨论】:

    标签: reactjs typescript web


    【解决方案1】:

    使用event.preventDefault()

       if (code === 'Enter') {
          if ([...value].filter((word) => word === '
    ').length >= DETAIL_MAX_LINES) {
            event.preventDefault();
          }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      相关资源
      最近更新 更多