removeEventListener 已弃用,我无法正确重构它

Har*_*ayn 19 refactoring dynamic-linking deprecated deep-linking react-native

Linking.removeEventListener('url', onReceiveURL);

removeEventListener 已弃用。

这是我的 IDE 的建议:

EventEmitter.removeListener('url', ...):方法已被弃用。请改为使用remove()所返回的订阅 EventEmitter.addListener

  // Custom function to subscribe to incoming links
  subscribe(listener: (deeplink: string) => void) {
    // First, you may want to do the default deep link handling
    const onReceiveURL = ({url}: {url: string}) => listener(url);
    // Listen to incoming links from deep linking
    Linking.addEventListener('url', onReceiveURL);
    const handleDynamicLink = (
      dynamicLink: FirebaseDynamicLinksTypes.DynamicLink,
    ) => {
      listener(dynamicLink.url);
    };
    const unsubscribeToDynamicLinks = dynamicLinks().onLink(handleDynamicLink);
    return () => {
      unsubscribeToDynamicLinks();
      Linking.removeEventListener('url', onReceiveURL);
    };
Run Code Online (Sandbox Code Playgroud)

我尝试了很多方法,但似乎没有任何效果。

没有找到任何有关它的具体信息。

有什么帮助解决吗?

编辑 - >我将进一步调查,但到目前为止它正在工作:

 const unsubscribeToDynamicLinks : any = ...

then in return : 
return () => {
unsubscribeToDynamicLinks().remove('url', onReceiveURL);};

 
Run Code Online (Sandbox Code Playgroud)

Ani*_*ita 39

对于新的 API,它的工作原理如下:


useEffect (() => {
  const subscription = Linking.addEventListener('url', onReceiveURL);
  return () => subscription.remove();
}, [])

Run Code Online (Sandbox Code Playgroud)


Cün*_*ğlu 8

对于类组件,您可以使用如下所示的内容:

class MyClass extends Component {
  constructor(props){
    super(props)
    this.changeEventListener = null
  }
  componentDidMount(){
    // or you can use constructor for this
    this.changeEventListener = AppState.addEventListener('change', () => {
      // your listener function
    })
  }

  componentWillUnmount() {
    this.changeEventListener.remove()
  }
}


Run Code Online (Sandbox Code Playgroud)