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

问题描述 投票:0回答:3

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);
    };

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

没有找到任何具体信息。

有什么帮助可以解决吗?

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

 const unsubscribeToDynamicLinks : any = ...

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

 
react-native refactoring deep-linking deprecated dynamic-linking
3个回答
29
投票

对于新 API,它是这样工作的:


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


6
投票

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

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()
  }
}



0
投票
  AppState.addEventListener('change', handleAppStateChange);
  return () => {
    AppState.removeEventListener('change', handleAppStateChange);
  };

由于removeEventListener已被弃用,所以我将上面的代码行替换为:

const 订阅 = AppState.addEventListener( '改变', 处理AppStateChange ); 返回()=> { 订阅.删除(); };

© www.soinside.com 2019 - 2024. All rights reserved.