Type 'Context' is missing the following properties from type 'Context<unknown>': Provider, Consumer TS2345

Issue

I am trying to use rootstore to access two different stores in my react Project.
RoorStore.ts =>

import ExtractionStore from "./extractionStore";
import UserStore from "./userStore";
import { createContext } from "vm";

export class RootStore {
  extractionStore: ExtractionStore;
  userStore: UserStore;

  constructor() {
    this.extractionStore = new ExtractionStore(this);
    this.userStore = new UserStore(this);
  }
}

export const RootStoreContext = createContext(new RootStore());

However while trying to inject it into my component I am getting an Error:

Component tsx =>

    const ExtractionDashboard: React.FC = () => {
      const rootStore = useContext(RootStoreContext);
      const { loadWorkList, loadingInitial } = rootStore.extractionStore;

Error:

Argument of type 'Context' is not assignable to parameter of type 'Context<unknown>'.
  Type 'Context' is missing the following properties from type 'Context<unknown>': Provider, Consumer  TS2345

     7 | 
     8 | const ExtractionDashboard: React.FC = () => {
  >  9 |   const rootStore = useContext(RootStoreContext);
       |                                ^
    10 |   const { loadWorkList, loadingInitial } = rootStore.extractionStore;
    11 | 
    12 |   useEffect(() => {

Solution

You are incorrectly importing the createContext function

What you have

import { createContext } from "vm";

What you should have

import { createContext } from "react";

Answered By – Dummy

Answer Checked By – Senaida (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.