不变违规:元素类型无效:预期为字符串或类/函数

时间:2019-10-30 06:26:51

标签: javascript react-native expo

我有一个EXPO应用程序,其功能应该像“相机”一样。我尝试了许多解决方案,但它们似乎没有用。

  

错误为:“不变违规:预期元素类型无效   字符串(对于内置组件)或类/函数(对于复合   元素),但有对象。检查“ CameraPage”的渲染方法。

App.js

import React from 'react';

import CameraPage from './src/camera.page';

export default class App extends React.Component {
    render() {
        return (
            <CameraPage />
        );
    };
};

camera.page.js

import React from 'react';
import { View, Text } from 'react-native';
import * as Camera from 'expo-camera';
import * as Permissions from 'expo-permissions'

import styles from './styles';

export default class CameraPage extends React.Component {
    camera = null;

    state = {
        hasCameraPermission: null,
    };

    async componentDidMount() {
        const camera = await Permissions.askAsync(Permissions.CAMERA);
        const audio = await Permissions.askAsync(Permissions.AUDIO_RECORDING);
        const hasCameraPermission = (camera.status === 'granted' && audio.status === 'granted');

        this.setState({ hasCameraPermission });
    };

    render() {
        const { hasCameraPermission } = this.state;

        if (hasCameraPermission === null) {
            return <View />;
        } else if (hasCameraPermission === false) {
            return <Text>Access to camera has been denied.</Text>;
        }

        return (
            <View>
                <Camera
                    style={styles.preview}
                    ref={camera => this.camera = camera}
                />
            </View>
        );
    };
};

1 个答案:

答案 0 :(得分:0)

您以错误的方式导入了Camera组件。正确的是:

import { Camera } from 'expo-camera';

请检查文档here并查看working demo