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

200
Visualizações
React.createContext throwing error when using Provider

I am trying to use React contexts to manage my states throughout child components in typescript. However, I currently face an issue with the following code:

Inside contexts/context.ts I have:

export const MyContext = React.createContext(null);

Inside components/Parent.tsx I have:

import {MyContext} from '../contexts/context';

export default function Parent() {
    const [state, setState] = React.useState([]);

    const MyContextManager = React.useContext(MyContext);

    return (
       <MyContextManager.Provider value={[state, setState]}>
          <Child />
       </MyContextManager.Provider>
    )  
 }

And inside my Child component I have:

const [state, setState] = React.useContext(MyContext);

const handleState = (value) => {
      setState(value);
}

However, I currently get an error:

TypeError: MyContextManager is null
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You meant to use MyContext.Provider. Check the documentation.

Also you mentioned using TypeScript, yet that TypeError is a runtime error. It seems like TS thinks your MyContextManager is of type any or so. That, or TypeScript thinks having null as a component type is fine.

about 4 years ago · Juan Pablo Isaza Relatório
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