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
You may pass params like so