简体   繁体   English

如何避免附加多个事件侦听器?

[英]How to avoid multiple event listeners from being attached?

Is the following code going to attach multiple event listeners or will React Native / expo-linking only allow one event listener be attached at a time?以下代码是要附加多个事件侦听器还是 React Native / expo-linking 一次只允许附加一个事件侦听器?

import * as Linking from 'expo-linking'
import { useIsFocused } from '@react-navigation/native'

const MyComponent = () => {
  const isFocused = useIsFocused()

  useEffect(() => {
    fetchData()
    Linking.addEventListener('url', _handleEvent)
  }, [isFocused])

  const fetchData = () => {
    // ...
  }

  const _handleEvent = () => {
    // ...
  }

  return (
    <View><View>
  )
}

Is there are a way to check if an event listener already exists so I can do something like:有没有办法检查事件侦听器是否已经存在,以便我可以执行以下操作:

useEffect(() => {
  fetchData()
  if(!eventListenerExists){
    Linking.addEventListener('url', _handleEvent)
  }
}, [isFocused])

It'll attach multiple handlers, adding one each time isFocused changes.它将附加多个处理程序,每次isFocused更改时添加一个。 To remove the previous handler when attaching the next, return a function that React will call:要在附加下一个处理程序时删除前一个处理程序,请返回一个 React 将调用的函数:

useEffect(() => {
  fetchData()
  Linking.addEventListener('url', _handleEvent)
  return () => Linking.removeEventListener('url', _handleEvent) // <======
}, [isFocused])

You want to do that anyway so that the handler is removed when your component is completely unmounted.无论如何都想这样做,以便在您的组件完全卸载时删除处理程序。

This is covered in the React documentation here .这在此处的 React 文档中有所介绍。

I think even should call one time我想甚至应该打一次电话

useEffect(() => {
  Linking.addEventListener('url', _handleEvent)
  return () => Linking.removeEventListener('url', _handleEvent) // <======
}, [])

1、in the useEffect callback, return the function that remove the listener; 1、在useEffect回调中,返回移除监听器的函数; 2、every time remove the listener before bind it. 2、每次在绑定之前删除监听器。 such as:如:

useEffect(() => {
    window.removeEventListener('url', hander);
    window.addEventListener('url', hander);
    return () => window.removeEventListener('url', hander);
}, [XXX])

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

相关问题 避免来自.focus的多个事件侦听器 - Avoid multiple event listeners from .focus 如何避免文本字段上的多个事件侦听器? - How to avoid multiple event listeners on a text field? 具有事件侦听器的多个图表 - 如何查找哪个图表触发了侦听器? 附上测试用例和截图 - Multiple charts with event listeners - how to find which chart has fired the listener? Test case and screenshot are attached 避免多次添加事件侦听器 - Avoid from adding event listeners several times 如何避免事件监听器共享 Scope - How To Avoid Event Listeners Shared Scope 我们可以使用redux在全局范围内订阅事件侦听器,以避免将作为道具的功能从父级传递到多个子级吗 - Can we subsribe event listeners globally using redux to avoid passing a function as a prop from parent to multiple child 如何/何时将事件侦听器附加到d3.js中? - How/when do event listeners get attached in d3.js? Chrome 开发工具:如何查看 HTML 元素的附加事件侦听器 - Chrome dev tools: How to see attached event listeners for an HTML element 阻止将新事件附加到元素 - Block new event from being attached to an element 如何防止使用jquery和socket.io添加多个事件侦听器 - How to prevent multiple event listeners being added with jquery and socket.io
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM