Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
Context returns undefined functions on any refresh in React Native App

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:

  • It is not a spelling error, or 'can't find file' error.
  • The only thing I am currently storing in session is the auth token.
  • Everything from state is defined (even if in its INITIAL_STATE form)
  • Wherever I call the undefined function, I use async/await
  • I do not actually use the Consumer anywhere
  • The AppWrapper, GlobalContext and ChildContext are all nested inside a context folder. App.js is in the root directory.
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda