在以下代码中:
componentDidMount() {
registerForPushNotificationsAsync();
this._notificationSubscription = Notifications.addListener(this._handleNotification);
}
将Notifications.addListener(this._handleNotification);
分配给this._notificationSubscription
和简单地进行操作之间有什么区别:
componentDidMount() {
registerForPushNotificationsAsync();
Notifications.addListener(this._handleNotification);
}
还要如何将其转换为如下所示的功能组件格式?
useEffect(() => {
registerForPushNotificationsAsync();
Notifications.addListener(handleNotification);
}, []);
答案 0 :(得分:0)
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()
}
}, []);