react-native/Libraries/Utilities/mountSafeCallback.js
Spencer Ahrens 6e179fb7cd [ReactNative] introduce mountSafeCallback
Summary:
`mountSafeCallback` simply wraps a callback in an `isMounted()` check to prevent crashes when old callbacks are called on unmounted components.

@public

Test Plan:
Added logging and made sure callbacks were getting called through
`mountSafeCallback` and that things worked (e.g. photo viewer rotation etc).
2015-05-13 18:36:53 -08:00

24 lines
638 B
JavaScript

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule mountSafeCallback
* @flow
*/
'use strict';
var mountSafeCallback = function(context: ReactComponent, callback: ?Function): any {
return function() {
if (!callback || !context.isMounted()) {
return;
}
return callback.apply(context, arguments);
};
};
module.exports = mountSafeCallback;