I am currently experiencing some issues with Firebase and the use of state. And yes, I would like to point out that I am new to React. Basically what I am trying to achieve, is to display all inquiries that have been sent to me, which are stored in firebase.
In an attempt to accomplish this, I have done as following:
constructor(props) {
super();
this.fetchMessage = this.fetchMessage.bind(this);
this.state = {
messages: []
};
}
fetchMessage = () => {
const database = firebase.database();
const inquiries = [];
database.ref('inquiries').on('value', (snapshot) => {
snapshot.forEach((childSnapshot) => {
inquiries.push({
id: childSnapshot.key,
fullName: childSnapshot.val().fullName,
email: childSnapshot.val().email,
message: childSnapshot.val().message,
subject: childSnapshot.val().subject,
dateSent: childSnapshot.val().dateSent
});
});
console.log(inquiries);
this.setState(() => {
return {
messages: inquiries
}
});
});
}
What I am trying to do is to store all inquiries in state so that I can run a for-loop and display all of the content on my website. How exactly would I do this?
My first idea was to, as said: store each object in state, and then access each inquiry like this this.state.messages[0], this.state.messages[1] (to get the full array), and then display each value by doing this.state.messages[0].fullName for instance.
How can I achieve this? My current error is:
[Error] Invariant Violation: Objects are not valid as a React child (found: object with keys {id, fullName, email, message, subject, dateSent}). If you meant to render a collection of children, use an array instead.
in h1 (created by AdminPanel)
in div (created by AdminPanel)
in AdminPanel (created by Route)
in Route
in Switch
in div
in Router (created by BrowserRouter)
in BrowserRouter
(anonymous function) (bundle.js:1530)