Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-native asking Android runtime permission

I'm trying to ask for Android permissions within my React Native app. I followed the official documentation here and I get this runtime error (from the react-native log-androidcommand) :

W ReactNativeJS: undefined is not an object (evaluating 'PermissionsAndroid.PERMISSIONS.CAMERA')

This is the code I actually have :

My import (generated by WebStorm) :

import * as PermissionsAndroid from "react-native";

The code that actually ask for the permission :

try {
        const granted = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.CAMERA,
            {
                'title': 'A title',
                'message': 'A message'
            }
        )
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
            return true;
        } else {
            return false;
        }
    } catch (err) {
        console.warn(err)
    }
}

I can't understand why this doesn't work since it comes from the official documentation.

like image 862
Antoine Auffray Avatar asked Feb 13 '26 17:02

Antoine Auffray


1 Answers

In short words: you should import a single member of react-native module. This will look like this:

import { PermissionsAndroid } from 'react-native';

What causes the problem is your import syntax. In your example you are importing the whole react-native module as PermissionAndroid variable in your module. But as you see from my code line you need only a single member from the whole package.

As for me, it was really a good idea to try different ES6 import syntax in real world and to get used to them.

like image 171
Roman Osypov Avatar answered Feb 16 '26 07:02

Roman Osypov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!