组件“div”的视图配置 getter 回调必须是一个函数(收到“未定义”)。确保组件名称以大写字母开头

Vib*_*uti 11 javascript data-retrieval react-native firebase-realtime-database

错误是:不变违规:组件“div”的查看配置 getter 回调必须是一个函数(收到“未定义”)。确保组件名称以大写字母开头。我在尝试从 firebase 检索数据到 React Native 的表组件时遇到此错误,即 ReactTable 并且在控制台中查看数据时还在控制台中提供一个空数组,因此输出中没有任何内容。

import React, { Component } from 'react';
import { StyleSheet, View, Text, Button } from 'react-native';
import ReactTable from 'react-table';
import firebase from 'firebase';


const firebaseConfig = {
...
};
firebase.initializeApp(firebaseConfig);


export default class Form1 extends Component {
constructor(props) {
    super(props);

    this.state = {
        data: [],
        columns: [
            {
                Header: "email",
                accessor: "email"
            },
            {
                Header: "password",
                accessor: "password"
            }
        ]
    }
}

componentDidMount() {
    const data = [];
    var query = firebase.database().ref("users");
    query.once("value").then((snapshot) => {
        snapshot.forEach((childSnapshot, index) => {
            let singleObj = {
                email: childSnapshot.val().email,
                password: childSnapshot.val().password,
            }
            data.push(singleObj);

            if (index === snapshot.length - 1) {
                this.setState({ data: data });
            }
        });
    });
}

render() {
    return (
        <div>
            {this.state.data.length > 0 && <ReactTable data={this.state.data} columns= 
{this.state.columns} />}
        </div>
    );
}
}

const styles = StyleSheet.create({
container: { flex: 1, padding: 16, paddingTop: 30, backgroundColor: '#fff' },
head: { height: 40, backgroundColor: '#f1f8ff' },
text: { margin: 6 }
});
Run Code Online (Sandbox Code Playgroud)

Shw*_*eta 72

有时,当您的组件导入不正确时,就会出现此错误。在我的react-native项目中,FlatList是从react-native-web而不是react-native框架导入的,这导致了上述错误。当我从反应本机框架导入它时,它工作得很好。

  • 这也是我的解决方案!Visual Studio Code 现在已从“react-native-web”错误导入了几次! (6认同)

Gos*_*dra 17

你不能在 React Native 中使用 div 用 View 改变它

改变

      <div>
            {this.state.data.length > 0 && <ReactTable data={this.state.data} columns= 
{this.state.columns} />}
        </div>
Run Code Online (Sandbox Code Playgroud)

        <View>
            {this.state.data.length > 0 && <ReactTable data={this.state.data} columns= 
{this.state.columns} />}
        </View>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!


小智 5

我遇到了类似的错误,上面写着

错误是:不变违规:组件“表单”的视图配置 getter 回调必须是一个函数(收到“未定义”)。

我的问题是,我使用的是来自 formik 的网络版本,所以我只需要使用 React Native 版本,它将表单更改为视图。

希望它对其他人有帮助。

  • 我得到了同样的错误,因为我从react-native-web而不是react-native导入了View。 (2认同)