@td-design/rn-hooks
Version:
从ahooks里面抽取的一些在rn项目里可以使用的hooks。提高开发效率
49 lines (45 loc) • 1.74 kB
JavaScript
;
var _tslib = require('../_virtual/_tslib.js');
var react = require('react');
var reactNative = require('react-native');
var index = require('../useDimensions/index.js');
var index$1 = require('../useSafeState/index.js');
var isOrientationPortrait = function (_a) {
var width = _a.width, height = _a.height;
return height >= width;
};
var isOrientationLandscape = function (_a) {
var width = _a.width, height = _a.height;
return width >= height;
};
function useDeviceOrientation() {
var screen = index().screen;
var initialState = {
portrait: isOrientationPortrait(screen),
landscape: isOrientationLandscape(screen),
};
var _a = _tslib.__read(index$1(initialState), 2), orientation = _a[0], setOrientation = _a[1];
react.useEffect(function () {
var onChange = function (_a) {
var screen = _a.screen;
setOrientation({
portrait: isOrientationPortrait(screen),
landscape: isOrientationLandscape(screen),
});
};
var subscription = reactNative.Dimensions.addEventListener('change', onChange);
return function () {
// @ts-ignore - React Native >= 0.65
if (typeof (subscription === null || subscription === void 0 ? void 0 : subscription.remove) === 'function') {
// @ts-ignore - need update @types/react-native@0.65.x
subscription.remove();
}
else {
// @ts-ignore React Native < 0.65
reactNative.Dimensions.removeEventListener('change', onChange);
}
};
}, [setOrientation]);
return orientation;
}
module.exports = useDeviceOrientation;