Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!