I want to use the React Hooks functionality in combination with Firebase. But now when the data is set, the result is only visible when the DOM is being updated. My current code is:
import React, { useState, useEffect } from 'react';
import firebase, { getPivotFunction } from '../../firebase';
/**
* Return a styled component
*/
const ListCards = () => {
const [data, setData] = useState([]);
const userListsRef = firebase
.database()
.ref('userLists')
.child('1234d343f');
useEffect(() => {
(async function() {
try {
const response = await getPivotFunction(userListsRef, 'lists');
setData(response);
} catch (e) {
console.error(e);
}
})();
}, []);
/**
* Return all list cards
*/
return (
<ul>
{data.map(item => (
<li key={item.time}>dummy text</li>
))}
</ul>
);
};
When the page is beeing rendered for the first time the 'dummy text' is not displayed, only when there is an update beeing triggered.
My goal is to let 'dummy text' apear when the page is done loading and data
not having a length of 0.
In this case getPivotFunction
contains:
/** Get FireBase data based on a pivot table */
const getPivotFunction = (ref, target) => {
let dataArray = [];
ref.once('value', userInfo => {
userInfo.forEach(function(result) {
firebase
.database()
.ref(target)
.child(result.key)
.on('value', snapshot => {
dataArray.push(snapshot.val());
});
});
});
return dataArray;
};
Please let me know