我在我的react + redux应用程序中使用打字稿。我的组件之一利用了react-redux
的连接。代码是这样的:
import * as React from 'react';
import * as Redux from 'redux'
import compose from 'recompose/compose';
import {
connect,
} from 'react-redux';
import withContextId from '../../../../../app/containers/pageTab/contexts/withContextId';
import {
fetchContent,
} from '../../actions/workspaceActions';
import { HomePageQuery } from '../../interfaces';
interface Props extends StateProps, DispatchProps {
queryType: string,
query: string,
contextId: string,
}
interface OwnProps {
queryType: string,
query: string,
contextId: string,
}
class ContentContainer extends React.PureComponent<Props, {}> {
componentDidMount() {
const { props } = this;
props.fetchContent(props.queryType, props.query, props.contextId);
}
render() {
return (
<div>
{'Tiles Container'}
</div>
);
}
}
interface DispatchProps {
fetchContent: (query: string, queryType: string, contextId: string) => void
}
function mapDispatchToProps(dispatch: Redux.Dispatch<any>): DispatchProps {
return {
fetchContent: (query: string, queryType: string, contextId: string) => {
dispatch(fetchContent(query, queryType, contextId))
}
};
}
interface StateProps {
}
function mapStateToProps(state: any): StateProps {
return {};
}
export default compose(
withContextId,
connect<StateProps, DispatchProps, OwnProps>(mapStateToProps, mapDispatchToProps),
)(ContentContainer);
我阅读了this的答案,并尝试将StateProps
,DispatchProps
和OwnProps
分开,但仍然会出现此错误。如何解决此错误?
[编辑]
我从其父母那里收到queryType
和query
的身份(这些都是必不可少的道具):
答案 0 :(得分:0)
我在临时withContextId
上遇到了问题。显然withContextId
是用JS编写的,没有任何类型。
删除compose
并执行以下操作可帮助我解决问题。一个拥有更多typescript
知识的人可以掌握它,并让我理解,因为我仍然是新手。
export default connect<StateProps, DispatchProps, OwnProps>(
mapStateToProps, mapDispatchToProps
)(withContextId(ContentContainer));