简体   繁体   English

使用 useRef 作为文本框来处理外部点击不起作用 - 反应功能组件

[英]using useRef for a textbox to handle outside click not working - React functional component

am just giving a minimal code to tell about my issue am facing now我只是给出一个最小的代码来告诉我现在面临的问题

I have a textbox我有一个文本框

which is being given as这是给出的

<input type="text" ref={inputRef}/>

and

  useEffect(() => {
    document.addEventListener("click", handleClickOutside, false);
    return () => {
      document.removeEventListener("click", handleClickOutside, false);
    };
  }, []);

  const handleClickOutside = event => {
    if (inputRef.current && !inputRef.current.contains(event.target)) {

    }
  };

But here am getting an error like TypeError: inputRef.current.contains is not a function但是这里出现类似TypeError: inputRef.current.contains is not a function的错误

Not sure how to check the same for input text box不知道如何检查输入文本框

Here what am trying to achieve is need to get control if user click outside the input text box Note: i had done the same for div and component, but for text box, not sure如果用户在输入文本框外部单击,则需要在这里实现控制注意:我对 div 和组件做了同样的事情,但对于文本框,不确定

Any help will be highly appreciated任何帮助将不胜感激

Below is the code snippet of a function to handle the outside click.下面是处理外部点击的 function 的代码片段。

const useOutsideClick = (ref, callback) => {
  useEffect(() => {
    const handleClickOutside = (event) => {
      if (ref.current && !ref.current.contains(event.target)) {
        callback();
      }
    };
    document.addEventListener('mousedown', handleClickOutside);
    return () => {
      document.removeEventListener('mousedown', handleClickOutside);
    };
  }, [ref, callback]);
};

Usage inside a component:组件内部的用法:

...
const inputRef = useRef(null);
useOutsideClick(inputRef, callbackToHandleOutsideClick);

return <input type="text" ref={inputRef}/>;
...

You have to use similar kind of code to make it work.您必须使用类似的代码才能使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React - useRef 与 TypeScript 和功能组件 - React - useRef with TypeScript and functional component 处理在React中单击外部组件 - Handle Click outside Component in React 如何检测React功能组件之外的点击 - How to detect click outside of the React functional component 使用反应挂钩 (useRef) 将 class 组件转换为功能组件—如何避免“未定义”错误 - Converting class component to functional component using react hooks (useRef)—how to avoid “undefined” error 在 React 功能组件中使用 useRef 时出现无法读取 null 错误的属性“样式” - Getting a Cannot read property 'style' of null Error When Using useRef Within a React Functional Component 与反应之外的功能组件通信 - communicating with a functional component outside of react clearInterval 在使用功能组件的 React 应用程序中不起作用 - clearInterval not working in React Application using functional component 如何在反应功能组件中正确使用 useRef 挂钩? - How to use useRef hook properly in react Functional Component? 在带有酶和玩笑的功能组件内模拟 React useRef 或 function? - Mock React useRef or a function inside a functional component with enzyme and jest? 使用钩子检测 React 组件外的点击 - Detect click outside React component using hooks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM