【发布时间】:2019-08-17 21:09:28
【问题描述】:
每当我从我的组件调用我的操作时,它都会向我显示错误。我正在使用带有 react redux 的打字稿。
错误:属性“getItem”不存在于类型“Readonly & Readonly'。
MY Action
import * as actionTypes from "../types";
import { getItems } from "../api/allApi";
export const getItem = () => {
const payload = getItems();
return {
type: actionTypes.GET_ITEM,
payload
};
};
My Component
import React from "react";
import "../styles/settings.scss";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import { getItem } from "../Actions/action";
class Settings extends React.Component {
componentDidMount() {
this.props.getItem(); // error here: Property 'getItem' does not exist on //type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'.ts(2339)
}
render() {
return (
<div>
{console.log(this.props.items)} // Error here: Property 'items' //does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'
{/* {console.log("asda", this.props.items)} */}
</div>
);
}
}
const mapStateToProps = (state: any) => {
return {
items: state.getItemsReducer
};
};
const mapDispatchToProps = (dispatch: any) => {
return bindActionCreators({ getItem }, dispatch);
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(Settings);
【问题讨论】:
-
另外,最好不要留下很多
any's。例如mapStateToProps的返回类型是IProps或Partial<IProps>,如果你决定填充更多的道具。此外,它的状态应该是state:{ getItemsReducer: soneType[]}。不确定实际输出,但您可以弄清楚...
标签: reactjs typescript redux