mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: All `removeEventListener` methods was removed https://github.com/facebook/react-native/commit/2596b2f6954362d2cd34a1be870810ab90cbb916 perviously but seems a `BackHandler` was missed This can be a breaking change for some third-party modules. **Migration**: Use `remove` on the EventSubscription from `addEventListener`: ```diff useEffect(()=>{ + const subscription = NativeModule.removeListener(name, listener); + return ()=>subscription.remove(); },[]) ``` ## Changelog: [GENERAL] [REMOVED] - Remove `BackHandler.removeEventListener` Pull Request resolved: https://github.com/facebook/react-native/pull/45892 Test Plan: ... Reviewed By: huntie Differential Revision: D65663591 Pulled By: javache fbshipit-source-id: 01b804cd6ec77ea4916a0ced7fee551d045f1684
46 lines
953 B
JavaScript
46 lines
953 B
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const _backPressSubscriptions = new Set();
|
|
|
|
const BackHandler = {
|
|
exitApp: jest.fn(),
|
|
|
|
addEventListener: function (
|
|
eventName: BackPressEventName,
|
|
handler: () => ?boolean,
|
|
): {remove: () => void} {
|
|
_backPressSubscriptions.add(handler);
|
|
return {
|
|
remove: () => {
|
|
_backPressSubscriptions.delete(handler);
|
|
},
|
|
};
|
|
},
|
|
|
|
mockPressBack: function () {
|
|
let invokeDefault = true;
|
|
const subscriptions = [..._backPressSubscriptions].reverse();
|
|
for (let i = 0; i < subscriptions.length; ++i) {
|
|
if (subscriptions[i]()) {
|
|
invokeDefault = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (invokeDefault) {
|
|
BackHandler.exitApp();
|
|
}
|
|
},
|
|
};
|
|
|
|
module.exports = BackHandler;
|