类型'Context'缺少类型'Context '中的以下属性:提供者,使用者TS2345

问题描述 投票:0回答:1

我正在尝试使用rootstore访问我的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());

但是尝试将其注入我的组件时出现错误:

Component tsx =>

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

错误:

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(() => {
reactjs typescript mobx
1个回答
0
投票

您正在正确导入createContext功能

您拥有的

import { createContext } from "vm";

您应该拥有的东西

import { createContext } from "react";
© www.soinside.com 2019 - 2024. All rights reserved.