【发布时间】:2021-02-18 09:48:26
【问题描述】:
有这么简单的React 组件:
import React from 'react';
import { connect } from 'react-redux';
import { INCREMENT, DECREMENT } from '../Misc/actions';
interface Props {
count: number,
asd: number
}
class Counter extends React.Component<Props> {
state = { count: 0 }
increment = () => {
this.props.dispatch({ type: "INCREMENT" });
}
decrement = () => {
this.props.dispatch({ type: "DECREMENT" });
}
render() {
...
)
}
}
type ICounterState = {
count: number
}
function mapStateToProps(state:ICounterState) {
return {
count: state.count,
asd: 78
};
}
export default connect(mapStateToProps)(Counter);
我收到一个错误:Property 'dispatch' does not exist on type 'Readonly<Props> & Readonly<{ children?: ReactNode; }>'。
我肯定是TypeScript 错误但是如何解决这个问题?
【问题讨论】:
-
您的界面
Props缺少调度。 -
@HMR 好的,我如何输入调度操作?
-
import {Dispatch} from 'react-redux'然后dispatch:Dispatch在接口 Props 中。
标签: reactjs typescript redux react-redux