[英]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.