Umi: 使用 umi+dva+typescript 和 class 组件,会在编辑器里报找不到 this.props.dispatch

Created on 29 Nov 2018  ·  9Comments  ·  Source: umijs/umi

代码是:

// pages/index.tsx
class Page extends Component {
  render(){
    return (
      <Button onClick={ () => { this.props.dispatch(...); } }>Button</Button>
    );
  }
}

报错的信息是:
Property 'dispatch' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>'.

Most helpful comment

image

All 9 comments

```
interface PageProps {
dispatch: any;
location: any;
message: any;
}
class Page extends Component {}

嗯,这个方法我找到了,但是要每个页面都写一遍。最省事的办法也是每个页面都写

class Page extends Component<any> {}

有更简便的方法吗?

用ts写类型定义不是必须的吗?
我问个,ts用的比较熟悉的朋友哈

image

我已经放弃用 typescript 了

huanglei1991 的方法可以

@smithyj 那你放弃了很多好东西~

@huanglei1991 这样确实可以,请问 Basic.BaseProps 是自己写的还是可以在 umi/dva 中引用的?

因为理论上能直接引用会最好,但目前我只在 dva 里找到了

export interface SubscriptionAPI {
  history: History,
  dispatch: Dispatch<any>,
}

而事实上已经发现的默认还有这些字段:route match location computedMatch children staticContext
所以如果想要包含这些 信息,可能要写:

import { SubscriptionAPI } from 'dva';
import { A } from 'a';
import { B } from 'b';
import { C } from 'c';

class Base<P = {}, S = {}> extends React.Component <
  P & SubscriptionAPI & A & B & C,
  S
>{}

@masquevil 也正在尝试typescript+umi+dva写项目 https://github.com/typescript-projects/react-admin-template 欢迎一起

Was this page helpful?
0 / 5 - 0 ratings

Related issues

y2891663091 picture y2891663091  ·  4Comments

afc163 picture afc163  ·  3Comments

zemzheng picture zemzheng  ·  3Comments

ddzy picture ddzy  ·  3Comments

mizi-lin picture mizi-lin  ·  3Comments