问题描述
我通过 create-react-app test --template redux-typescript 创建我的项目,然后我通过连接而不是钩子重写 /src/features/counter/Counter.tsx,然后 vscode 给出这个错误。
import React,{ Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { RootState,Appdispatch } from '../../app/store';
import {
decrement,increment,incrementByAmount,incrementAsync,incrementIfOdd,} from './counterSlice';
import styles from './Counter.module.css';
const mapState = (state: RootState) => {
return {
count: state.counter.value
}
}
const mapdispatch = (dispatch: Appdispatch) => {
return {
decrement: bindActionCreators(decrement,dispatch),increment: bindActionCreators(increment,incrementByAmount: bindActionCreators(incrementByAmount,incrementAsync: bindActionCreators(incrementAsync,incrementIfOdd: bindActionCreators(incrementIfOdd,}
}
type TypeofMapState = ReturnType<typeof mapState>;
type TypeofMapdispatch = ReturnType<typeof mapdispatch>;
type Ipros = TypeofMapState & TypeofMapdispatch;
class CounterComp extends Component<Ipros> {
state = {
incrementAmount: '2'
}
setIncrementAmount = (incrementAmount: string) => {
this.setState({ incrementAmount });
}
render() {
...
}
}
const Counter = connect<TypeofMapState,TypeofMapdispatch>(mapState,mapdispatch)(CounterComp);
export {
Counter
}
我不知道怎么了,我只添加了带有 TypeofMapState 和 Typeofdispatch 的连接类型参数。
解决方法
使用
const Counter = connect<TypeofMapState,TypeofMapDispatch,{},RootState>(mapState,mapDispatch)(CounterComp);
解决这个错误