类型“上下文”缺少类型“上下文<未知>”中的以下属性:提供者,使用者TS2345

时间:2019-12-23 18:18:42

标签: reactjs typescript mobx

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

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

组件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(() => {

2 个答案:

答案 0 :(得分:0)

您正在正确导入func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) { // Call your read functions here. BLE.sharedInstance.read(fromCharacteristic: BLE.sharedInstance.RBL_CHAR_SCN_UUID) } 函数

你有什么

createContext

你应该拥有的东西

import { createContext } from "vm";

答案 1 :(得分:0)

我遇到过这个错误,但不是因为导入不正确。就我而言,当我不应该这样做时,我试图为我的(解构的)上下文对象分配一个类型。

interface ContextInterface {
    name: string;
}

const Context = createContext<ContextInterface>({ name: 'John Smith' });

//error here: Type 'ContextInterface' is missing the following properties from type 'Context<ContextInterface>': Provider, Consumer ts(2739)
const { name }: React.Context<ContextInterface> = useContext<ContextInterface>(Context);

删除 React.Context<ContextInterface> 解决了错误。

//no error
const { name } = useContext<ContextInterface>(Context);