Javascript 如何将基于类的生命周期方法转换为函数方法?
在以下代码中:Javascript 如何将基于类的生命周期方法转换为函数方法?,javascript,reactjs,react-native,class,expo,Javascript,Reactjs,React Native,Class,Expo,在以下代码中: componentDidMount(){ registerForPushNotificationsAsync(); this.\u notificationSubscription=Notifications.addListener(this.\u handleNotification); } 分配Notifications.addListener(此.\u handleNotification)与分配Notifications.addListener有什么区别到这个。_not
componentDidMount(){
registerForPushNotificationsAsync();
this.\u notificationSubscription=Notifications.addListener(this.\u handleNotification);
}
分配Notifications.addListener(此.\u handleNotification)与分配Notifications.addListener有什么区别
到这个。_notificationSubscription
然后简单地去:
componentDidMount(){
registerForPushNotificationsAsync();
Notifications.addListener(此.\u handleNotification);
}
另外,如何将其转换为如下所示的功能组件格式
useffect(()=>{
registerForPushNotificationsAsync();
通知。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()
}
}, []);