useEffect doesn't get called before children methods do. Is that normal? (react native)…
- User
useEffect doesn't get called before children methods do. Is that normal? (react native) and how can I give it a default value before children methods start relying on the data
import { createContext, useEffect, useState } from "react"; import FoodData from "./FoodData";
type FoodDataContextType = { foodData: any[], refreshData: () => void; };
export const FoodDataContext = createContext({foodData: [], refreshData: () => {}});
export const FoodDataProvider = ({ children }) => { const [foodData, setFoodData] = useState([]); const refreshData = () => { // Pretend we called the API, got the last 5 days const date = new Date(); let newData = []; [...Array(1)].map((x, i) => { // API response let posts = []; // Temp offset the date var prevDate = new Date(); prevDate.setDate(date.getDate() - i); for (let i = 0; i < 10; i++) { posts[i] = new FoodData('Buckeye Donuts', 'go to buckeye donuts and get a free donut with a student id because when you go and you get a student id they will give you af re donut because it sa special thig today and joh cena will be there and he gies every student with an a on their report card a high 5 and', 'lat and long lol', prevDate.getTime(), true, true, true); } // Fill in array newData[i] = { title: prevDate.toLocaleDateString(undefined, { month: 'long', day: 'numeric' }), index: i, data: posts, }; }); // Update global data setFoodData(newData); console.log("NEW DATA!!!", newData); } useEffect(() => { // Fetch data when the component mounts console.log("HI"); refreshData(); }, []); return ( <FoodDataContext.Provider value={{ foodData, refreshData }}> {children} </FoodDataContext.Provider> ); }
- Assistant