developer_user
developer_user

Reputation: 543

Error: element type is invalid expected a string (for built-in components) or a class/function (for composite elements)

I am fetching the data from the firebase and try to display all the data in table format.

I found that ReactTable is a good way to print all the retrieved data in columns and rows and so I choose this method to print data in table form. But it is unable to print the data in columns and row and instead it is showing the following error:

Error: element type is invalid expected a string (for built-in components) or a class/function (for composite elements)

Please correct the code if it is wrong and try to resolve my issue, since i am trying this code from so long and still not able to solve the mistake.

import React, { Component } from 'react';
import { StyleSheet, View, Text } from 'react-native';

import ReactTable from 'react-table';


import firebase from 'firebase';


const firebaseConfig = {
apiKey: "XXXXX",
authDomain: "XXXXX",
databaseURL: "XXXXX",
projectId: "XXXXX",
storageBucket: "XXXXX",
messagingSenderId: "XXXXX",
appId: "XXXX",
measurementId: "XXXX"
};
firebase.initializeApp(firebaseConfig);
export default class Form1 extends Component {
constructor(props) {
super(props);

this.state = {
   data: []
}


}
componentDidMount() {
firebase.database().ref('users').on('value',snapshot => {
   const data = [];
   snapshot.forEach((childSnapShot) => {
    const locker = {
        email: child.val().email,
        password: child.val().password,
       // price: child.val().price,
     };
     data.push(locker);
    });
   this.setState(prevState => {
    return { data: [...prevState.data, ...data] };
  });
  console.log(this.state.data)
});
}

render() {
const columns = [
  {
    Header: "email",
    accessor: "email"
  },
  {
    Header: "password",
    accessor: "password"
  }
  ];
return (
   <div>
    <ReactTable data={this.state.data} columns={columns} />
  </div>
);
}
}

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

Upvotes: 0

Views: 444

Answers (1)

Neetin Solanki
Neetin Solanki

Reputation: 681

I found some small mistakes in your code and corrected it. I think you should follow Firebase and React documentation first, as the mistakes were of very basic level. Let me know if this answer helps you.

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

const firebaseConfig = {
    apiKey: "XXXXX",
    authDomain: "XXXXX",
    databaseURL: "XXXXX",
    projectId: "XXXXX",
    storageBucket: "XXXXX",
    messagingSenderId: "XXXXX",
    appId: "XXXX",
    measurementId: "XXXX"
};
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 (
            <View>
                {this.state.data.length > 0 && <ReactTable data={this.state.data} columns={this.state.columns} />}
            </View>
        );
    }
}

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

Upvotes: 1

Related Questions