小编Luu*_*sen的帖子

将 React 组件作为参数时,“类型缺少以下属性、上下文、setState”

我的用例非常简单,我想要一个动态反应组件并将其保存在注册表中:

import { Component } from 'react';
import DummyComponent from './DummyComponent';

class Registry {
  TestComponent: Component = DummyComponent;
}
Run Code Online (Sandbox Code Playgroud)

DummyComponent.tsx:

import { Component } from 'react';

export default class DummyComponent extends Component {
  constructor(props: any) {
    super(props);
    throw new Error('Cannot use this dummy component');
  }
}
Run Code Online (Sandbox Code Playgroud)

这引发了 TypeScript 错误: ts(2740): Type 'typeof DummyComponent' is missing the following properties from type 'Component<{}, {}, any>': context, setState, forceUpdate, render, and 3 more.

我究竟做错了什么?我只想Registry.TestComponent成为一个 React 组件,它不必拥有所有这些属性,因为 React 组件不需要它们。

我做了一个StackBlitz 示例 …

typescript reactjs

7
推荐指数
1
解决办法
9090
查看次数

标签 统计

reactjs ×1

typescript ×1