I'm trying to conditionally render a component using a useState
prop when it is finished loading in my useEffect
hook but I am having issues. I've tested my theory thus far and am able to render parts of the data once it is finished loading but I can't pass it as a prop to my component and when it is passed, it says it is undefined in the component. I'm trying to get the component to be able to render when the data has finished loading from Firestore.
Its throwing me for a loop that i cant render my component but i can conditionally render the other blocks below.
here is my code:
export default function Dashboard() {
const [keys, setKeys] = useState(allKeys);
const [user, loading, error] = useAuthState(auth);
const [dataToShow, setData] = useState();
const actionsPath = `Users/${user.uid}/XXXXXXXXX/`;
let arrayActions = [];
useEffect(() => {
onSnapshot(collection(db, actionsPath), (snapshot) => {
const docs = snapshot.docs;
docs.forEach((doc) => arrayActions.push(doc.data())); // this works
setData(arrayActions);
});
}, []);
return (
<React.Fragment>
<h1>Dashboard</h1>
<h1>{user.displayName}</h1>
<h1>{user.uid}</h1>
<h2>
<strong>Stat</strong>
</h2>
{/* able to conditionally render */}
{dataToShow ? <p>Data is here</p> : <p>nothing</p>}
{/* able to show the data */}
{dataToShow ? dataToShow?.map((doc, index) => (
<h1>{doc.Add}</h1>
)) : <p>nothing</p>}
{/* block below shows an error and gives error: `Uncaught TypeError: csvData is not iterable`*/}
{dataToShow ? <Profile
propsData={dataToShow} {/* shows undefined in console log in Profile Component */}
keys={keys}
colors={colors}
height="600px"
/> : <p>nothing</p>}
</React.Fragment>
);
Any and all help and direction is appreciated.