react-native-location-enabler
Version:
This package makes it easy for an React Native App to ensure that the android device's system settings are properly configured for the app's location needs. If your app needs to request location, the device needs to enable the appropriate system s
37 lines (31 loc) • 1.46 kB
JavaScript
import { useState, useEffect, useCallback } from 'react';
import { NativeModules, NativeEventEmitter, Platform } from 'react-native';
const {
LocationEnabler
} = NativeModules;
const EVENT_NAME = 'onChangeLocationSettings'; // Override
const locationEnabler = new NativeEventEmitter(LocationEnabler);
LocationEnabler.addListener = (listener, context) => Platform.OS === 'android' ? locationEnabler.addListener(EVENT_NAME, listener, context) : () => {};
LocationEnabler.once = (listener, context) => locationEnabler.once(EVENT_NAME, listener, context);
LocationEnabler.PRIORITIES = LocationEnabler.getConstants();
LocationEnabler.useLocationSettings = (settings, initial) => {
const [enabled, setEnabled] = useState(initial || undefined);
const callback = useCallback(() => {
const listner = LocationEnabler.addListener(_ref => {
let {
locationEnabled
} = _ref;
return setEnabled(locationEnabled);
});
LocationEnabler.checkSettings(settings);
if (enabled) listner.remove();else return listner;
}, [enabled, settings]);
useEffect(() => {
const listner = callback();
return () => listner === null || listner === void 0 ? void 0 : listner.remove();
}, [callback]);
const requestResolutionSettings = useCallback(() => LocationEnabler.requestResolutionSettings(settings), [settings]);
return [enabled, requestResolutionSettings];
};
export default LocationEnabler;
//# sourceMappingURL=index.js.map