简体   繁体   English

如何将基于类的生命周期方法转换为函数式方法?

[英]How to convert a class-based lifecycle method to a functional method?

In the following code:在以下代码中:

  componentDidMount() {
    registerForPushNotificationsAsync();
    this._notificationSubscription = Notifications.addListener(this._handleNotification);
  }

what's the difference between assigning Notifications.addListener(this._handleNotification);分配Notifications.addListener(this._handleNotification);什么区别Notifications.addListener(this._handleNotification); to this._notificationSubscription and simply going:this._notificationSubscription并简单地去:

  componentDidMount() {
    registerForPushNotificationsAsync();
    Notifications.addListener(this._handleNotification);
  }

Also, how do you convert it into a functional component format like the following?另外,您如何将其转换为如下所示的功能组件格式?

    useEffect(() => {
        registerForPushNotificationsAsync();
        Notifications.addListener(handleNotification);
    }, []);
useEffect(() => {
        registerForPushNotificationsAsync();
        const subscription =Notifications.addListener(handleNotification);
        return () => {
            // Call method for clear subscription. I asume that it is remove but it can be another
            subscription.remove()
        }
    }, []);

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

相关问题 如何将基于 React 类的组件转换为函数式组件? - How to convert a React class-based component into a functional component? 将基于反应类的组件转换为基于功能的组件 - convert react class-based to functional based component 如何将基于类的组件中的函数转换为 React 中的函数式组件? - How can I convert functions in class-based component to functional components in React? 如何将其转换为基于类的Javascript? - How can I convert this to class-based Javascript? 在 React 中将外部库从基于类迁移到函数式组件 - Moving external library from Class-based to Functional Component in React 为什么使用无状态功能组件而不是基于类的组件? - Why use stateless functional Components instead of class-based Components? 想将 class 组件转换为功能组件它有两个渲染方法所以我很困惑如何转换它 - want to convert class component into functional component it has two render method so i am confused how to convert it 如何在功能 typescript class 中导出方法? - How to export a method in a functional typescript class? React.js:如何将基于类的组件转换为函数式? - React.js: How to convert class based component to functional? 如何在 Javascript/Typescript 中创建基于类的枚举 - How to create Class-based Enums in Javascript/Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM