I have an issue on any app refresh, ONE of my Contexts is returning undefined functions.
I have the following structure:
export default function AppWrapper() {
return (
<GlobalProvider>
<ChildProvider>
<App />
</ChildProvider>
</GlobalProvider>
);
}
I am getting undefined on functions from ChildProvider, which looks like this:
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 };
And in Home.js, I call these functions as such:
const { getDataFromStore } = useContext(ChildContext)
useEffect(() => {
const getOrders = async () => {
store?.name && (await getDataFromStore());
};
getOrders();
}, [store]);
And in Home.js is where that function (and any function defined in ChildContext) is undefined on any refresh.
Some Details:
auth token.INITIAL_STATE form)AppWrapper, GlobalContext and ChildContext are all nested inside a context folder. App.js is in the root directory.