Expo Camera仅使用React Navigation打开一次

bor*_*ral 8 navigation camera native reactjs expo

我通过反应导航将Expo相机设置为在中间选项卡上打开。但是,只有在我第一次单击该选项卡时,相机才会打开。如果我关闭它然后再返回,那只是一个黑屏。拍照按钮也不存在。(我是整体上本机和有点编码的新手)

'use strict';
import React, { Component } from 'react';
import { createBottomTabNavigator } from 'react-navigation';
import { Camera, Permissions } from 'expo';
import {
    AppRegistry,
    Dimensions,
    StyleSheet,
    Text,
    TouchableOpacity,
    View,
    Button
} from 'react-native';


class HomeScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>Home!</Text>
      </View>
    );
  }
}

class CameraView extends React.Component {
  state = {
    hasCameraPermission: null,
    type: Camera.Constants.Type.back,
  };

  async componentWillMount() {
    const { status } = await Permissions.askAsync(Permissions.CAMERA);
    this.setState({ hasCameraPermission: status === 'granted' });
  }
  render() {
     const { hasCameraPermission } = this.state;
    if (hasCameraPermission === null) {
      return <View />;
    } else if (hasCameraPermission === false) {
      return <Text>No access to camera</Text>;
    } else {
      return (
        <View style={{ flex: 1 }}>
          <Camera style={{ flex: 1 }} type={this.state.type}>
            <View
              style={{
                flex: 1,
                backgroundColor: 'transparent',
                flexDirection: 'row',
              }}>
              <TouchableOpacity
                style={{
                  flex: 0.1,
                  alignSelf: 'flex-end',
                  alignItems: 'center',
                }}
                onPress={() => {
                  this.setState({
                    type: this.state.type === Camera.Constants.Type.back
                      ? Camera.Constants.Type.front
                      : Camera.Constants.Type.back,
                  });
                }}>
                <Text
                  style={{ fontSize: 18, marginBottom: 10, color: 'white' }}>
                  {' '}Flip{' '}
                </Text>
              </TouchableOpacity>
            </View>
          </Camera>
        </View>
      );
    }
  }
}

class SettingsScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>Settings!</Text>
      </View>
    );
  }
}


export default createBottomTabNavigator({
  Home: HomeScreen,
  Camera:CameraView,
  Settings: SettingsScreen
});

const styles = StyleSheet.create({
    container: {
        flex: 1,
        flexDirection: 'column',
        top: 250
    },

    capture: {
        flex: 0,
        backgroundColor: '#fff',
        borderRadius: 5,
        padding: 15,
        paddingHorizontal: 20,
        alignSelf: 'center',
        margin: 20
    }

});
Run Code Online (Sandbox Code Playgroud)

Sel*_*rim 9

使用反应导航 5.x

import { useIsFocused } from '@react-navigation/native';

export const CameraView = (props) => {
    const isFocused = useIsFocused();
    return (
       <View>
         { isFocused && <RNCamera  />  }
       </View>
     )
}
Run Code Online (Sandbox Code Playgroud)


小智 7

要完成这项工作,您需要:

1.

import { NavigationEvents } from 'react-navigation';
Run Code Online (Sandbox Code Playgroud)
state = { loaded: true }
Run Code Online (Sandbox Code Playgroud)
render() {
    const { loaded } = this.state;
        return (
            <View style={styles.container}>
            <NavigationEvents
             onWillFocus={payload => this.setState({loaded: true})}
             onDidBlur={payload => this.setState({loaded: false})}/>
            <View style={styles.cameraArea}>
            {loaded && (
                <Camera
                 type={Camera.Constants.Type.back}
                 ref={ref => {
                     this.camera = ref;
                }}
              />
              )}          
            </View>
Run Code Online (Sandbox Code Playgroud)

这个想法是隐藏这个相机视图(onDidBlur->加载:false),然后当你回来时(onWillFocus被触发并将加载更改为true)。当调用 render() 函数时,它将<Camera />再次显示。如果您有任何问题随时问。


小智 6

我有一些问题。

这段代码为我解决了这个问题:

import { withNavigationFocus } from 'react-navigation'

render() {
    const { isFocused } = this.props
    return (
       <View>
         { isFocused && <RNCamera  ... />  }
       </View
     )
}


export default withNavigationFocus(Camera)
Run Code Online (Sandbox Code Playgroud)