Tengo un problema con la actualización de cualquier aplicación, UNO de mis Contextos está devolviendo funciones indefinidas.
tengo la siguiente estructura:
export default function AppWrapper() { return ( <GlobalProvider> <ChildProvider> <App /> </ChildProvider> </GlobalProvider> ); } Me estoy volviendo undefined en las funciones de ChildProvider , que se ve así:
const INITIAL_STATE = { orders: [], error: null, }; const ChildContext = React.createContext({ ...INITIAL_STATE }); const ChildConsumer = ChildContext.Consumer; const OrderProvider = (props) => { const { children } = props; const { store, userId } = useContext(GlobalContext); const [state, setState] = React.useReducer((state, newState) => { const combinedState = { ...state, ...newState }; return combinedState; }, INITIAL_STATE); const getDataFromStore = async () => { if (store?.id) { try { const { error, data } = await getOrdersByStore({store.id}); if (data) { const data1 = <iterating through data>; await setState({ orders: data1 }); } else { await setState({ error: error }); } } catch (e) { await setState({ error: e }); } } }; const actions = { getDataFromStore}; return <ChildContext.Provider value={{ ...state, ...actions }}>{children}</ChildContext.Provider>; }; export { ChildContext, ChildConsumer, ChildProvider }; Y en Home.js , llamo a estas funciones como tales:
const { getDataFromStore } = useContext(ChildContext) useEffect(() => { const getOrders = async () => { store?.name && (await getDataFromStore()); }; getOrders(); }, [store]); Y en Home.js es donde esa función (y cualquier función definida en ChildContext ) no está undefined en ninguna actualización.
Algunos detalles:
auth token .INITIAL_STATE )AppWrapper, GlobalContext and ChildContext están anidados dentro de una carpeta de context . App.js está en el directorio raíz.