skip to Main Content

I want to check the camera and microphone permission at the beginning, and pass the permission status to permiss page if permissions are not granted.

here is the app.tsx

import AppNavigation from './navigation/appNavigation';

function App() {
  return AppNavigation();
}

export default App;

the navigations:

import React, {useEffect, useCallback, useState} from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import HomeScreen from '../screens/HomeScreen';
import NotImplementScreen from '../screens/NotImplementScreen';
import PermissionsPage from '../screens/Permission';
import {Platform} from 'react-native';
import {request, check, PERMISSIONS, RESULTS} from 'react-native-permissions';
import {useNavigation} from '@react-navigation/native';

const Stack = createNativeStackNavigator();
// prettier-ignore
export default function AppNavigation() {
  const [cameraPermission, setCameraPermission] = useState("unavailable");
  const [microphonePermission, setMicrophonePermission] = useState("unavailable");

  useEffect(() => {
    if (Platform.OS === 'ios')
    {
      check(PERMISSIONS.IOS.CAMERA).then((result) => {setCameraPermission(result);});
      check(PERMISSIONS.IOS.MICROPHONE).then((result) => {setMicrophonePermission(result);});
    } else if (Platform.OS === 'android')
    {
      check(PERMISSIONS.ANDROID.CAMERA).then((result) => {setCameraPermission(result);});
      check(PERMISSIONS.ANDROID.RECORD_AUDIO).then((result) => {setMicrophonePermission(result);});
    }
  }, []);

  console.log(
    `Re-rendering Navigator. Camera: ${cameraPermission} | Microphone: ${microphonePermission}`,
  );

  if (cameraPermission == null || microphonePermission == null) {
    // still loading
    return null;
  }

  const showPermissionsPage = cameraPermission != "granted" || microphonePermission != "granted";

  return (
    <NavigationContainer>
       <Stack.Navigator
        initialRouteName={showPermissionsPage ? 'Permissions' : 'Home'} >
        <Stack.Screen
          options={{headerShown: false}}
          name="Home"
          component={HomeScreen}
        />
        <Stack.Screen
          options={{headerShown: false}}
          name="Not Implement"
          component={NotImplementScreen}
        />
        <Stack.Screen
          options={{headerShown: false}}
          name="Permissions"
          component={PermissionsPage}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

that means when cameraPermission and microphonePermission changed , I want to pass these two permissions to PermissionsPage

if I use initialParams like this:

<Stack.Screen
          options={{headerShown: false}}
          name="Permissions"
          component={PermissionsPage}
initialParams={{cameraPermissionStatus:cameraPermission}}
        />

the parameter only valid when screen is initialized, but at that time cameraPermission is not ready.
how can I pass cameraPermission to PermissionsPage when it changed

2

Answers


    1. You can use useFocus hook to check the permission state when the user return fom settings screen to your app.
    2. To spread the value across the app you can use context
    Login or Signup to reply.
  1. You may pass params like so

    <Stack.Screen
      options={{headerShown: false}}
      name="Permissions"
      children={() => <PermissionsPage cameraPermissionStatus={cameraPermission} />}
    />
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search